📄 directoryinfo.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 1 'Persistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "DirectoryInfo"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
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: DirectoryInfo
'
''
' Represents a directory to modify and retrieve information about.
'
' @remarks The DirectoryInfo object does not verify the existence
' of the directory until information has to be read or written to that directory.
'
' @see Constructors
'
Option Explicit
Implements IObject
Implements FileSystemInfo
Private mPath As String
Private mOriginalPath As String
Private mIsDirty As Boolean
Private mFileAttributes As Long
Private mCreationTime As cDateTime
Private mLastAccessTime As cDateTime
Private mLastWriteTime As cDateTime
''
' Returns the name of the directory this instance represents.
'
' @param The name of the directory.
'
Public Property Get Name() As String
Name = Path.GetFileName(mPath)
If Len(Name) = 0 Then Name = mPath
End Property
''
' Returns the name of the directory, including the full path to the directory.
'
' @param The full path to the directory.
'
Public Property Get FullName() As String
FullName = mPath
End Property
''
' Returns the directory's attributes (ReadOnly, Hidden, Archive, ...)
'
' @return The directories attributes since the last time Refresh was called.
' @remarks The attributes are not kept current. If the they are changed since
' last checked, then Refresh must be called to retrieve the current settings.
'
Public Property Get Attributes() As FileAttributes
If mIsDirty Then Call Refresh
Attributes = mFileAttributes
End Property
''
' Sets the attributes for the directory.
'
' @param RHS The attributes to set for the directory.
' @remarks Setting just one attribute will clear all the other attributes. To
' preserve any attributes, then OR must be used with the attributes to
' be preserved.
'
Public Property Let Attributes(ByVal RHS As FileAttributes)
Call File.SetAttributes(mPath, RHS)
Call MarkDirty
End Property
''
' Returns the creation time for this directory in local time.
'
' @return A cDateTime object representing the creation time for this directory.
' @remarks The time returned is the UTC time with the timezone offset applied.
' For Pacific Coast, a -8 hours is added to the time before returning.
'
Public Property Get CreationTime() As Variant
If mIsDirty Then Refresh
Set CreationTime = mCreationTime
End Property
''
' Sets the creation time for the directory in local time.
'
' @param RHS A Date of the new time.
' @remarks The time has the UTC timezone offset applied to it before
' being written to the directory.
'
Public Property Let CreationTime(ByVal RHS As Variant)
Set Me.CreationTime = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the creation time for the directory in local time.
'
' @param RHS A cDateTime object of the new time.
' @remarks The time has the UTC timezone offset applied to it before
' being written to the directory.
'
Public Property Set CreationTime(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "CreationTime")
Call Directory.SetCreationTime(mPath, mCreationTime)
Call MarkDirty
End Property
''
' Returns the creation time as UTC time.
'
' @return A cDateTime object of the UTC time.
' @remarks The time returned is the time on the directory without
' any offsets being applied.
'
Public Property Get CreationTimeUtc() As Variant
If mIsDirty Then Refresh
Set CreationTimeUtc = mCreationTime.ToUniversalTime
End Property
''
' Sets the creation time as UTC time.
'
' @param RHS A Date value as the new time.
' @remarks The time has the UTC timezone offset removed from it before
' being written to the directory.
'
Public Property Let CreationTimeUtc(ByVal RHS As Variant)
Set Me.CreationTimeUtc = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the creation time as UTC time.
'
' @param RHS A cDateTime object of the new time.
' @remarks The time has the UTC timezone offset removed from it before
' being written to the directory.
'
Public Property Set CreationTimeUtc(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "CreationTime")
Call Directory.SetCreationTimeUtc(mPath, RHS)
Call MarkDirty
End Property
''
' Returns the last time the directory was accessed in local time.
'
' @return A cDateTime object of the time.
' @remarks The returned time is the UTC time with the UTC timezone offset applied.
'
Public Property Get LastAccessTime() As Variant
If mIsDirty Then Refresh
Set LastAccessTime = mLastAccessTime
End Property
''
' Sets the time the directory was last accessed in local time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Let LastAccessTime(ByVal RHS As Variant)
Set Me.LastAccessTime = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the time the directory was last accessed in local time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Set LastAccessTime(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "LastAccessTime")
Call Directory.SetLastAccessTime(mPath, mLastAccessTime)
Call MarkDirty
End Property
''
' Returns the last time the directory was accessed in UTC time.
'
' @return a cDateTime object of the time.
'
Public Property Get LastAccessTimeUtc() As Variant
If mIsDirty Then Refresh
Set LastAccessTimeUtc = mLastAccessTime.ToUniversalTime
End Property
''
' Sets the last time the directory was accessed in UTC time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Let LastAccessTimeUtc(ByVal RHS As Variant)
Set Me.LastAccessTimeUtc = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the last time the directory was accessed in UTC time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Set LastAccessTimeUtc(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "LastAccessTime")
Call Directory.SetLastAccessTimeUtc(mPath, RHS)
Call MarkDirty
End Property
''
' Returns the last time the directory was written to in local time.
'
' @return A cDateTime object of the time.
'
Public Property Get LastWriteTime() As Variant
If mIsDirty Then Refresh
Set LastWriteTime = mLastWriteTime
End Property
''
' Sets the last time the directory was written to in local time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Let LastWriteTime(ByVal RHS As Variant)
Set Me.LastAccessTime = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the last time the directory was written to in local time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Set LastWriteTime(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "LastWriteTime")
Call Directory.SetLastWriteTime(mPath, mLastWriteTime)
Call MarkDirty
End Property
''
' Returns the last time the directory was written to in UTC time.
'
' @return A cDateTime object of the time.
'
Public Property Get LastWriteTimeUtc() As Variant
If mIsDirty Then Refresh
Set LastWriteTimeUtc = mLastWriteTime.ToUniversalTime
End Property
''
' Sets the last time the directory was written to in UTC time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Let LastWriteTimeUtc(ByVal RHS As Variant)
Set Me.LastWriteTimeUtc = cDateTime.GetcDateTime(RHS)
End Property
''
' Sets the last time the directory was written to in UTC time.
'
' @param RHS A Date or cDateTime object of the new time.
'
Public Property Set LastWriteTimeUtc(ByVal RHS As Variant)
If Not TypeOf RHS Is cDateTime Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_DateRequired), "LastAccessTime")
Call Directory.SetLastWriteTimeUtc(mPath, RHS)
Call MarkDirty
End Property
''
' Reloads all of the attributes and timestamps for the directory.
'
Public Sub Refresh()
Dim Data As WIN32_FILE_ATTRIBUTE_DATA
Dim e As Long
e = File.GetFileData(mPath, Data)
If e <> NO_ERROR Then IOError e, mPath
With Data
mFileAttributes = .dwFileAttributes
Set mCreationTime = cDateTime.FromFileTime(.ftCreationTime)
Set mLastAccessTime = cDateTime.FromFileTime(.ftLastAccessTime)
Set mLastWriteTime = cDateTime.FromFileTime(.ftLastWriteTime)
End With
mIsDirty = False
End Sub
''
' Checks if the directory actually exists.
'
' @return An indication of the directory existing.
'
Public Property Get Exists() As Boolean
Exists = Directory.Exists(mPath)
End Property
''
' Returns an empty string for directories.
'
' @return An empty string.
'
Public Property Get Extension() As String
' do nothing
End Property
''
' Returns the parent directory as a DirectoryInfo object.
'
' @return A DirectroyInfo object of the parent directory, or Nothing if
' the original directory is a root, such as 'c:\'
Public Property Get Parent() As DirectoryInfo
Dim p As String
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -