📄 fileinfo.cls
字号:
'
Public Property Get Length() As Currency
If mIsDirty Then Refresh
Length = mFileSize
End Property
''
' Returns the filename portion of the path.
'
' @return The filename and extension.
'
Public Property Get Name() As String
Name = Path.GetFileName(mFileName)
End Property
''
' Reloads the file attributes and times.
'
' @remarks This will verify that the file exists.
'
Public Sub Refresh()
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
Dim e As Long
e = File.GetFileData(mFileName, Data)
If e <> NO_ERROR Then IOError e, mFileName
With Data
mFileAttributes = .dwFileAttributes
Set mCreationTime = cDateTime.FromFileTime(.ftCreationTime)
Set mLastAccessTime = cDateTime.FromFileTime(.ftLastAccessTime)
Set mLastWriteTime = cDateTime.FromFileTime(.ftLastWriteTime)
End With
' The nFileSizeLow and hFileSizeHigh are in reverse order in
' WIN32_FIND_DATA, so we need to manually reverse the 4 byte sections.
Dim Ptr As Long
Ptr = VarPtr(mFileSize)
MemLong(Ptr) = Data.nFileSizeLow
MemLong(Ptr + 4) = Data.nFileSizeHigh
mFileSize = 10000@ * mFileSize
mIsDirty = False
End Sub
''
' Opens a StreamWriter for the file.
'
' @return A Stream writer to the file.
'
Public Function AppendText() As StreamWriter
Set AppendText = File.AppendText(mFileName)
End Function
''
' Copies this file to a new location, with optional overwrite.
'
' @param DestFileName The new filename this file will be copied as.
' @param OverWrite Permission to overwrite an existing file.
' @remarks The destination must be on the same volume as this file.
'
Public Sub CopyTo(ByVal DestFileName As String, Optional ByVal OverWrite As Boolean)
Call File.Copy(mFileName, DestFileName, OverWrite)
End Sub
''
' Creates this file and returns a FileStream object to access it.
'
' @return A FileStream object used to access the created file.
' @remarks If the file does not exist it will be created. If it already exists, it will be overwritten.
'
Public Function Create() As FileStream
Set Create = File.Create(mFileName)
End Function
''
' Returns a StreamWriter to write to this file.
'
' @return The StreamWriter for writing to this file.
' @remarks If the file does not exist it will be created. If it already exists, it will be overwritten.
'
Public Function CreateText() As StreamWriter
Set CreateText = File.CreateText(mFileName)
End Function
''
' Deletes this file.
'
Public Sub Delete()
Call File.Delete(mFileName)
End Sub
''
' Moves this file to a new location.
'
' @param DestFileName The new location and name of the file.
' @remarks The new location must be on the same volume as the source filename.
'
Public Sub MoveTo(ByVal DestFileName As String)
Call File.Move(mFileName, DestFileName)
End Sub
''
' Opens the file as a FileStream.
'
' @param Mode The method of opening the file.
' @param Access The read/write operations allowed by this FileStream.
' @param Share The read/write operations allows by other processes.
' @return A FileStream object to this file.
'
Public Function OpenFile(ByVal Mode As FileMode, Optional ByVal Access As FileAccess = ReadWriteAccess, Optional ByVal Share As FileShare = FileShare.None) As FileStream
Set OpenFile = File.OpenFile(mFileName, Mode, Access, Share)
End Function
''
' Returns a FileStream in ReadOnly mode.
'
' @return A FileStream that has ReadOnly access to the file.
'
Public Function OpenRead() As FileStream
Set OpenRead = File.OpenRead(mFileName)
End Function
''
' Returns a StreamReader to this file.
'
' @return A StreamReader for reading the file.
' @remarks The StreamReader's default encoding is used.
'
Public Function OpenText() As StreamReader
Set OpenText = File.OpenText(mFileName)
End Function
''
' Returns a FileStream with write access to the file.
'
' @return A FileStream used to write to the file.
'
Public Function OpenWrite() As FileStream
Set OpenWrite = File.OpenWrite(mFileName)
End Function
''
' Returns a string representation of this object instance.
'
' @return String representing this instance.
Public Function ToString() As String
ToString = mFileName
End Function
''
' Returns a boolean indicating if the value and this object
' instance are the same instance.
'
' @param value The value to compare equality to.
' @return Boolean indicating equality.
Public Function Equals(ByRef Value As Variant) As Boolean
Equals = Object.Equals(Me, Value)
End Function
''
' Returns a pseudo-unique number identifying this instance.
'
' @return Pseudo-unique number identifying this instance.
Public Function GetHashCode() As Long
GetHashCode = ObjPtr(CUnk(Me))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Friend Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Friend Sub Init(ByVal FileName As String)
mOriginalFileName = FileName
mFileName = Path.GetFullPath(FileName)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Private Helpers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MarkDirty()
mIsDirty = True
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Class Events
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
Call MarkDirty
End Sub
Private Sub Class_ReadProperties(PropBag As PropertyBag)
Call Init(PropBag.ReadProperty("FileName"))
End Sub
Private Sub Class_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("FileName", mOriginalFileName)
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IObject Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IObject_Equals(Value As Variant) As Boolean
IObject_Equals = Equals(Value)
End Function
Private Function IObject_GetHashcode() As Long
IObject_GetHashcode = GetHashCode
End Function
Private Function IObject_ToString() As String
IObject_ToString = ToString
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FileSystemInfo Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Property Get FileSystemInfo_Attributes() As FileAttributes
FileSystemInfo_Attributes = Attributes
End Property
Private Property Let FileSystemInfo_Attributes(ByVal RHS As FileAttributes)
Attributes = RHS
End Property
Private Property Set FileSystemInfo_CreationTime(ByVal RHS As Variant)
Set CreationTime = RHS
End Property
Private Property Let FileSystemInfo_CreationTime(ByVal RHS As Variant)
CreationTime = RHS
End Property
Private Property Get FileSystemInfo_CreationTime() As Variant
Set FileSystemInfo_CreationTime = CreationTime
End Property
Private Property Set FileSystemInfo_CreationTimeUtc(ByVal RHS As Variant)
Set CreationTimeUtc = RHS
End Property
Private Property Let FileSystemInfo_CreationTimeUtc(ByVal RHS As Variant)
CreationTimeUtc = RHS
End Property
Private Property Get FileSystemInfo_CreationTimeUtc() As Variant
Set FileSystemInfo_CreationTimeUtc = CreationTimeUtc
End Property
Private Sub FileSystemInfo_Delete()
Call Delete
End Sub
Private Function FileSystemInfo_Equals(Value As Variant) As Boolean
FileSystemInfo_Equals = Equals(Value)
End Function
Private Property Get FileSystemInfo_Exists() As Boolean
FileSystemInfo_Exists = Exists
End Property
Private Property Get FileSystemInfo_Extension() As String
FileSystemInfo_Extension = Extension
End Property
Private Property Get FileSystemInfo_FullName() As String
FileSystemInfo_FullName = FullName
End Property
Private Function FileSystemInfo_GetHashCode() As Long
FileSystemInfo_GetHashCode = GetHashCode
End Function
Private Property Set FileSystemInfo_LastAccessTime(ByVal RHS As Variant)
Set LastAccessTime = RHS
End Property
Private Property Get FileSystemInfo_LastAccessTime() As Variant
Set FileSystemInfo_LastAccessTime = LastAccessTime
End Property
Private Property Let FileSystemInfo_LastAccessTime(ByVal RHS As Variant)
LastAccessTime = RHS
End Property
Private Property Set FileSystemInfo_LastAccessTimeUtc(ByVal RHS As Variant)
Set LastAccessTimeUtc = RHS
End Property
Private Property Let FileSystemInfo_LastAccessTimeUtc(ByVal RHS As Variant)
LastAccessTimeUtc = RHS
End Property
Private Property Get FileSystemInfo_LastAccessTimeUtc() As Variant
Set FileSystemInfo_LastAccessTimeUtc = LastAccessTimeUtc
End Property
Private Property Set FileSystemInfo_LastWriteTime(ByVal RHS As Variant)
Set LastWriteTime = RHS
End Property
Private Property Let FileSystemInfo_LastWriteTime(ByVal RHS As Variant)
LastWriteTime = RHS
End Property
Private Property Get FileSystemInfo_LastWriteTime() As Variant
Set FileSystemInfo_LastWriteTime = LastWriteTime
End Property
Private Property Set FileSystemInfo_LastWriteTimeUtc(ByVal RHS As Variant)
Set LastWriteTimeUtc = RHS
End Property
Private Property Let FileSystemInfo_LastWriteTimeUtc(ByVal RHS As Variant)
LastWriteTimeUtc = RHS
End Property
Private Property Get FileSystemInfo_LastWriteTimeUtc() As Variant
Set FileSystemInfo_LastWriteTimeUtc = LastWriteTimeUtc
End Property
Private Property Get FileSystemInfo_Name() As String
FileSystemInfo_Name = Name
End Property
Private Sub FileSystemInfo_Refresh()
Call Refresh
End Sub
Private Function FileSystemInfo_ToString() As String
FileSystemInfo_ToString = ToString
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -