📄 file.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "File"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2005 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: File
'
''
' Provides methods for manipulating and retrieving file information.
'
' @remarks This class cannot be directly created. To access the methods
' use the variable name directly.
'
' <pre>
' Debug.Print File.Exists("MyFile.txt")
' </pre>
'
Option Explicit
Private mDirectorySeparators() As Integer
''
' Opens a file to be written to starting at the end of the file.
'
' @param Path The name of the file to be appended to.
' @return A StreamWriter object used to write to the file.
' @remarks The StreamWriter uses a UTF8Encoding declared as Cor.NewUTF8Encoding(False, True).
' @see StreamWriter
'
Public Function AppendText(ByVal Path As String) As StreamWriter
Set AppendText = Cor.NewStreamWriter(Path, Append:=True)
End Function
''
' Append a text string to the end of a specified file using the supplied encoding.
'
' @param Path The file to append the text to.
' @param Contents The string to append to the file.
' @param Encoding The encoding used to encode the contents (Default UTF8).
' @remarks This method opens a file, appends the contents then closes the
' file in the call. If multiple lines need to be appended, then using this
' method may have performance problems.
' <p>If the file doesn't exist it will be created. If the file already
' exists, it will be overwritten.</p>
'
Public Sub AppendAllText(ByVal Path As String, ByVal Contents As String, Optional ByVal Encoding As Encoding)
Dim sw As StreamWriter
Set sw = Cor.NewStreamWriter(Path, Encoding, Append:=True)
Call sw.WriteValue(Contents)
Call sw.CloseWriter
End Sub
''
' Copies a file.
'
' @param SourceFileName The file to be copied.
' @param DestinationFileName The location and name of the copied file.
' @param OverWrite Indicates if the file already exists it should be overwritten.
'
Public Sub Copy(ByVal SourceFileName As String, ByVal DestinationFileName As String, Optional ByVal OverWrite As Boolean = False)
SourceFileName = cString.Trim(SourceFileName)
DestinationFileName = cString.Trim(DestinationFileName)
If Len(SourceFileName) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyFileName), "SourceFileName")
If Len(DestinationFileName) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyFileName), "DestinationFileName")
Call Path.VerifyPath(SourceFileName)
Call Path.VerifyPath(DestinationFileName)
SourceFileName = Path.GetFullPath(cString.TrimEnd(SourceFileName, mDirectorySeparators))
DestinationFileName = Path.GetFullPath(cString.TrimEnd(DestinationFileName, mDirectorySeparators))
If StrComp(SourceFileName, DestinationFileName, vbTextCompare) = 0 Then Exit Sub
If API.CopyFile(SourceFileName, DestinationFileName, Not OverWrite) = BOOL_FALSE Then
Dim e As Long
e = Err.LastDllError
If e <> ERROR_FILE_EXISTS Then
If Not File.Exists(SourceFileName) Then _
IOError ERROR_FILE_NOT_FOUND, SourceFileName
If Directory.Exists(DestinationFileName) Then _
Throw Cor.NewIOException(Environment.GetResourceString(IOException_DirectoryExists))
End If
IOError e, SourceFileName
End If
End Sub
''
' Creates a file. If the file already exists, it is overwritten.
'
' @param Path The new file to be created.
' @param BufferSize The size of the internal buffer used byte the FileStream object.
' @return A FileStream object to be used for reading and writing to the file.
' @remarks The FileStream is declared as Cor.NewFileStream(Path, FileMode.Create, FileAccess.ReadWriteAccess, FileShare.None, BufferSize).
' @see FileStream
'
Public Function Create(ByVal Path As String, Optional ByVal BufferSize As Long = 4096) As FileStream
Set Create = Cor.NewFileStream(Path, FileMode.Create, FileAccess.ReadWriteAccess, FileShare.None, BufferSize)
End Function
''
' Creates a file using a UTF8Encoding. If the file already exists, then it is overwritten.
'
' @param Path The new file to be created.
' @return A StreamWriter to be used to write to the new file.
' @remarks The StreamWriter is declared as Cor.NewStreamWriter(Path).
' @see StreamWriter
'
Public Function CreateText(ByVal Path As String) As StreamWriter
Set CreateText = Cor.NewStreamWriter(Path)
End Function
''
' Deletes a file.
'
' @param Path The file to be deleted.
' @remarks If the file did not exist, nothing happens.
'
Public Sub Delete(ByVal Path As String)
If Len(Path) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "Path")
Call VBCorLib.Path.VerifyPath(Path)
If API.DeleteFile(Path) = BOOL_FALSE Then
Dim Result As Long
Result = Err.LastDllError
If Result <> ERROR_FILE_NOT_FOUND Then IOError Result, Path
End If
End Sub
''
' Determines if a file exists.
'
' @param Path The file to check for existence.
' @return Whether the file exists or not.
'
Public Function Exists(ByVal Path As String) As Boolean
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
If GetFileData(Path, Data) = NO_ERROR Then
Exists = Not CBool(Data.dwFileAttributes And FileAttributes.DirectoryAttr)
End If
End Function
''
' Returns the file attributes (ReadOnly, Hidden, ...)
'
' @param Path The file to retrieve the attributes for.
' @return A value with individual bits representing if an attribute is applied to the file.
' @remarks To determine if a specific attribute is being used, the return value will
' need to be ANDed with the specific FileAttributes flag.<br>
' <pre>
' If Attr And FileAttributes.ReadOnly Then
' ''... file is readonly
' End If
' </pre>
'
Public Function GetAttributes(ByVal Path As String) As FileAttributes
Path = cString.Trim(Path)
If Len(Path) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "Path")
GetAttributes = API.GetFileAttributes(Path)
If GetAttributes = INVALID_FILE_ATTRIBUTES Then IOError Err.LastDllError, Path
End Function
''
' Returns the time the file was created in local time.
'
' @param Path The file to retrieve the creation time of.
' @return A cDateTime object representing the creation time.
'
Public Function GetCreationTime(ByVal Path As String) As cDateTime
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
Call FillFileData(Path, Data)
Set GetCreationTime = cDateTime.FromFileTime(Data.ftCreationTime)
End Function
''
' Returns the time the file was created in UTC time.
'
' @param sPath The file to retrieve the creation time of.
' @return A cDateTime object representing the creation time.
'
Public Function GetCreationTimeUtc(ByVal sPath As String) As cDateTime
Set GetCreationTimeUtc = GetCreationTime(sPath).ToUniversalTime
End Function
''
' Returns the time the file was accessed in local time.
'
' @param sPath The file to retrieve the last access time for.
' @return A cDateTime object that represents the last time the file was accessed.
'
Public Function GetLastAccessTime(ByVal sPath As String) As cDateTime
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
Call FillFileData(sPath, Data)
Set GetLastAccessTime = cDateTime.FromFileTime(Data.ftLastAccessTime)
End Function
''
' Returns The last time a file was accessed in UTC time.
'
' @param sPath The file to get the last accessed time for.
' @return A cDateTime object representing the last time the file was accessed.
'
Public Function GetLastAccessTimeUtc(ByVal sPath As String) As cDateTime
Set GetLastAccessTimeUtc = GetLastAccessTime(sPath).ToUniversalTime
End Function
''
' Returns the last time the file was written to in local time.
'
' @param sPath The file to retrieve the last written time for.
' @return A cDateTime object representing the last time the file was written to.
'
Public Function GetLastWriteTime(ByVal sPath As String) As cDateTime
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
Call FillFileData(sPath, Data)
Set GetLastWriteTime = cDateTime.FromFileTime(Data.ftLastWriteTime)
End Function
''
' Returns the last time the file was written to in UTC time.
'
' @param sPath The file to retrieve the last written time for.
' @return A cDateTime object representing the last time a file was written to.
'
Public Function GetLastWriteTimeUtc(ByVal sPath As String) As cDateTime
Set GetLastWriteTimeUtc = GetLastWriteTime(sPath).ToUniversalTime
End Function
''
' Moves a file from one location to another.
'
' @param SourceFileName The file to be moved.
' @param DestinationFileName The location and filename the file is to be moved to.
'
Public Sub Move(ByVal SourceFileName As String, ByVal DestinationFileName As String)
Call Path.VerifyPath(SourceFileName)
Call Path.VerifyPath(DestinationFileName)
If Len(SourceFileName) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "SourceFileName")
If Len(DestinationFileName) = 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "DestinationFileName")
If Not File.Exists(SourceFileName) Then IOError ERROR_FILE_NOT_FOUND, SourceFileName
If API.MoveFile(SourceFileName, DestinationFileName) = BOOL_FALSE Then IOError Err.LastDllError, DestinationFileName
End Sub
''
' Opens a file with a FileStream
'
' @param Path The file to be opened.
' @param Mode The mode in which to open the file.
' @param Access The type of access allowed by the FileStream.
' @param Share The access allowed for other processes.
' @return A FileStream object of the specified file.
'
Public Function OpenFile(ByVal Path As String, ByVal Mode As FileMode, Optional ByVal Access As FileAccess = -1, Optional ByVal Share As FileShare = FileShare.None) As FileStream
Set OpenFile = Cor.NewFileStream(Path, Mode, Access, Share)
End Function
''
' Opens a file for reading as a FileStream.
'
' @param Path The file to be opened in read mode.
' @return A FileStream used to read from the file.
' @remarks The FileStream object is declared as Cor.NewFileStream(Path, FileMode.OpenExisting, FileAccess.ReadAccess, FileShare.ReadShare).
'
Public Function OpenRead(ByVal Path As String) As FileStream
Set OpenRead = Cor.NewFileStream(Path, FileMode.OpenExisting, FileAccess.ReadAccess, FileShare.ReadShare)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -