新聞| | PChome| 登入
2007-10-25 22:55:03 | 人氣1,010| 回應0 | 上一篇 | 下一篇
推薦 0 收藏 0 轉貼0 訂閱站台

讀取檔案的建立時間及存取時間

來源:王國榮

想要進一步讀取檔案的相關資訊, 必須先呼叫 API 函數的 OpenFile 取得檔案的
Handle, 然後再利用 Handle 呼叫 GetFileInformationByHandle 讀取檔案的相關
資訊, 而在讀取的檔案相關資訊中便含有檔案建立、修改、及存取時間, 程式執
行過程如下:(假設想讀取的檔案是"c:\autoexec.bat")

REF 更改檔案日期時間 

Public Const OFS_MAXPATHNAME = 128
Public Const OF_READ = &H0
Type OFSTRUCT
    cBytes As Byte
    fFixedDisk As Byte
    nErrCode As Integer
    Reserved1 As Integer
    Reserved2 As Integer
    szPathName(OFS_MAXPATHNAME) As Byte
End Type

Type SYSTEMTIME
     wYear As Integer
     wMonth As Integer
     wDayOfWeek As Integer
     wDay As Integer
     wHour As Integer
     wMinute As Integer
     wSecond As Integer
     wMilliseconds As Integer
End Type

Type FileTime
     dwLowDateTime As Long
     dwHighDateTime As Long
End Type


Type BY_HANDLE_FILE_INFORMATION
     dwFileAttributes As Long
     ftCreationTime As FileTime
     ftLastAccessTime As FileTime
     ftLastWriteTime As FileTime
     dwVolumeSerialNumber As Long
     nFileSizeHigh As Long
     nFileSizeLow As Long
     nNumberOfLinks As Long
     nFileIndexHigh As Long
     nFileIndexLow As Long
End Type

Type TIME_ZONE_INFORMATION
     bias As Long
     StandardName(32) As Integer
     StandardDate As SYSTEMTIME
     StandardBias As Long
     DaylightName(32) As Integer
     DaylightDate As SYSTEMTIME
     DaylightBias As Long
End Type


Declare Function GetTimeZoneInformation Lib "kernel32" _
	(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, _
	lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As _
	Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As _
	FileTime, lpSystemTime As SYSTEMTIME) As Long

Dim FileHandle As Long
Dim FileInfo As BY_HANDLE_FILE_INFORMATION
Dim lpReOpenBuff As OFSTRUCT, ft As SYSTEMTIME
Dim tZone As TIME_ZONE_INFORMATION

Dim dtCreate As Date ' 建立時間
Dim dtAccess As Date ' 存取日期
Dim dtWrite As Date ' 修改時間
Dim bias As Long

' 先取得 autoexec.bat 的 File Handle
FileHandle = OpenFile("c:\autoexec.bat", lpReOpenBuff, OF_READ)
' 利用 File Handle 讀取檔案資訊
Call GetFileInformationByHandle(FileHandle, FileInfo)
Call CloseHandle(FileHandle)
' 讀取 Time Zone 資訊, 因為上一步驟的檔案時間是「格林威治」時間
Call GetTimeZoneInformation(tZone)
bias = tZone.bias ' 時間差, 以「分」為單位
Call FileTimeToSystemTime(FileInfo.ftCreationTime, ft) ' 轉換時間資料結構
dtCreate = DateSerial(ft.wYear, ft.wMonth, ft.wDay) + ;
	   TimeSerial(ft.wHour, ft.wMinute - bias, ft.wSecond)
Call FileTimeToSystemTime(FileInfo.ftLastAccessTime, ft)
dtAccess = DateSerial(ft.wYear, ft.wMonth, ft.wDay) + ;
	   TimeSerial(ft.wHour, ft.wMinute - bias,ft.wSecond)
Call FileTimeToSystemTime(FileInfo.ftLastWriteTime, ft)
dtWrite = DateSerial(ft.wYear, ft.wMonth, ft.wDay) +
	   TimeSerial(ft.wHour, ft.wMinute - bias,ft.wSecond)

'執行以上程式所得到的 dtCreate、dtWrite、及 dtAccess 變數, 即分別為檔案
'建立、修改、及存取時間。

                                                        

台長: Kenny
人氣(1,010) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: 程式設計 |
此分類下一篇:如何取得特殊資料夾的所在目錄?
此分類上一篇:設定/取消 網路磁碟機

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文