【VB声明】 Private Declare Function GetFileTime Lib "kernel32" Alias "GetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long【说明】 取得指定文件的时间信息 【返回值】 Long,非零表示成功,零表示失败。会设置GetLastError 【备注】 如果不需要特定的信息,那么lpCreationTime,lpLastAccessTime,lpLastWriteTime都可以设置为零(用ByVal As Long)。这个函数返回的文件时间采用UTC格式【参数表】 hFile ---------- Long,文件的句柄 lpCreationTime - FILETIME,用于装载文件的创建时间 lpLastAccessTime - FILETIME,用于装载文件上一次访问的时间(FAT文件系统不支持这一特性) lpLastWriteTime - FILETIME,用于装载文件上一次修改的时间
if tview.Selectitem is nothing then 'no selected else 'selected endif
'This program needs a Dialog box, named CDBox1 ' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T) ' and select Microsoft Common Dialog control) Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private Type SHFILEOPSTRUCT hWnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAborted As Boolean hNameMaps As Long sProgress As String End Type Private 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 Private Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 Private Const FO_DELETE = &H3 Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As Long) As Long Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long Private Sub Form_Load() Dim lngHandle As Long, SHDirOp As SHFILEOPSTRUCT, lngLong As Long Dim Ft1 As FILETIME, Ft2 As FILETIME, SysTime As SYSTEMTIME 'Set the dialog's title CDBox.DialogTitle = "Choose a file ..." 'Raise an error when the user pressed cancel CDBox.CancelError = True 'Show the 'Open File'-dialog CDBox.ShowOpen 'Create a new directory CreateDirectory "C:\KPD-Team", ByVal &H0 'Copy the selected file to our new directory CopyFile CDBox.filename, "C:\KPD-Team\" + CDBox.FileTitle, 0 'Rename the file MoveFile "C:\KPD-Team\" + CDBox.FileTitle, "C:\KPD-Team\test.kpd" 'Open the file lngHandle = CreateFile("C:\KPD-Team\test.kpd", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0) 'Get the file's size MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes." 'Get the fil's time GetFileTime lngHandle, Ft1, Ft1, Ft2 'Convert the file time to the local file time FileTimeToLocalFileTime Ft2, Ft1 'Convert the file time to system file time FileTimeToSystemTime Ft1, SysTime MsgBox "The selected file was created on" + Str$(SysTime.wMonth) + "/" + Ltrim(Str$(SysTime.wDay)) + "/" + Ltrim(Str$(SysTime.wYear)) 'Close the file CloseHandle lngHandle 'Delete the file DeleteFile "C:\KPD-Team\test.kpd" With SHDirOp .wFunc = FO_DELETE .pFrom = "C:\KPD-Team" End With 'Delete the directory SHFileOperation SHDirOp End End Sub
'bas Option Explicit Public Const OFS_MAXPATHNAME = 260 Public Const OF_READWRITE = &H2Public Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End TypePublic Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End TypePublic 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 TypePublic 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 TypePublic Declare Sub GetLocalTime Lib "kernel32" _ (lpSystemTime As SYSTEMTIME)Public Declare Function GetFileTime Lib "kernel32" _ (ByVal hFile As Long, lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, _ lpLastWriteTime As FILETIME) As Long
Public Declare Function SetFileTime Lib "kernel32" _ (ByVal hFile As Long, _ lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, _ lpLastWriteTime As FILETIME) As LongPublic Declare Function FileTimeToSystemTime Lib "kernel32" _ (lpFileTime As FILETIME, _ lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" _ (lpSystemTime As SYSTEMTIME, _ lpFileTime As FILETIME) As LongPublic Declare Function OpenFile Lib "kernel32" _ (ByVal lpFileName As String, _ lpReOpenBuff As OFSTRUCT, _ ByVal wStyle As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _ (ByVal hFile As Long) As LongPublic Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End TypePublic Const SEE_MASK_INVOKEIDLIST = &HC Public Const SEE_MASK_NOCLOSEPROCESS = &H40 Public Const SEE_MASK_FLAG_NO_UI = &H400Public Declare Function ShellExecuteEx Lib "shell32" _ Alias "ShellExecuteEx" _ (SEI As SHELLEXECUTEINFO) As Long Public Function GetFileDateString(CT As FILETIME) As String Dim ST As SYSTEMTIME Dim ds As Single
'convert the passed FILETIME to a 'valid SYSTEMTIME format for display If FileTimeToSystemTime(CT, ST) Then ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay) GetFileDateString = Format$(ds, "DDDD MMMM D, YYYY") Else: GetFileDateString = "" End IfEnd Function Public Function GetSystemDateString(ST As SYSTEMTIME) As String Dim ds As Single
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
If ds Then GetSystemDateString = Format$(ds, "DDDD MMMM D, YYYY") Else: GetSystemDateString = "error!" End IfEnd Function '--end block--''formOption ExplicitPrivate Sub Command2_Click() 'variables required Dim hFile As Long Dim fName As String Dim tmp As String
'structures required Dim OFS As OFSTRUCT Dim SYS_TIME As SYSTEMTIME Dim FT_CREATE As FILETIME Dim FT_ACCESS As FILETIME Dim FT_WRITE As FILETIME Dim NEW_TIME As FILETIME
'assign the textbox entry to the filename fName = (Text1)
'open the file hFile = OpenFile(fName, OFS, OF_READWRITE)
'get the FILETIME info for the created, 'accessed and last write info Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only --------------------------- 'show the system time info tmp = "Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf tmp = tmp & "Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf tmp = tmp & "Last Modified:" & vbTab & GetFileDateString(FT_WRITE) Text2.Text = tmp '--------------------------------------------
'obtain the local system time '(adjusts for the GMT deviation 'of the local time zone) GetLocalTime SYS_TIME
'convert the system time to a valid file time Call SystemTimeToFileTime(SYS_TIME, NEW_TIME)
'set the created, accessed and modified dates all 'to the new dates. A null (0&) could be passed as 'any of the NEW_TIME parameters to leave that date unchanged. Call SetFileTime(hFile, NEW_TIME, NEW_TIME, NEW_TIME)
're-read the updated FILETIME info for the created, 'accessed and last write info Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only --------------------------- 'show the system time info tmp = "New Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf tmp = tmp & "New Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf tmp = tmp & "New Last Modified:" & vbTab & GetFileDateString(FT_WRITE) Text4.Text = tmp '--------------------------------------------
'clean up by closing the file Call CloseHandle(hFile)End Sub
Private Declare Function GetFileTime Lib "kernel32" Alias "GetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long【说明】
取得指定文件的时间信息 【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError 【备注】
如果不需要特定的信息,那么lpCreationTime,lpLastAccessTime,lpLastWriteTime都可以设置为零(用ByVal
As Long)。这个函数返回的文件时间采用UTC格式【参数表】
hFile ---------- Long,文件的句柄 lpCreationTime - FILETIME,用于装载文件的创建时间 lpLastAccessTime - FILETIME,用于装载文件上一次访问的时间(FAT文件系统不支持这一特性) lpLastWriteTime - FILETIME,用于装载文件上一次修改的时间
'no selected
else
'selected
endif
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private 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
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FO_DELETE = &H3
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As Long) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Sub Form_Load()
Dim lngHandle As Long, SHDirOp As SHFILEOPSTRUCT, lngLong As Long
Dim Ft1 As FILETIME, Ft2 As FILETIME, SysTime As SYSTEMTIME
'Set the dialog's title
CDBox.DialogTitle = "Choose a file ..."
'Raise an error when the user pressed cancel
CDBox.CancelError = True
'Show the 'Open File'-dialog
CDBox.ShowOpen
'Create a new directory
CreateDirectory "C:\KPD-Team", ByVal &H0
'Copy the selected file to our new directory
CopyFile CDBox.filename, "C:\KPD-Team\" + CDBox.FileTitle, 0
'Rename the file
MoveFile "C:\KPD-Team\" + CDBox.FileTitle, "C:\KPD-Team\test.kpd"
'Open the file
lngHandle = CreateFile("C:\KPD-Team\test.kpd", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
'Get the file's size
MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."
'Get the fil's time
GetFileTime lngHandle, Ft1, Ft1, Ft2
'Convert the file time to the local file time
FileTimeToLocalFileTime Ft2, Ft1
'Convert the file time to system file time
FileTimeToSystemTime Ft1, SysTime
MsgBox "The selected file was created on" + Str$(SysTime.wMonth) + "/" + Ltrim(Str$(SysTime.wDay)) + "/" + Ltrim(Str$(SysTime.wYear))
'Close the file
CloseHandle lngHandle
'Delete the file
DeleteFile "C:\KPD-Team\test.kpd"
With SHDirOp
.wFunc = FO_DELETE
.pFrom = "C:\KPD-Team"
End With
'Delete the directory
SHFileOperation SHDirOp
End
End Sub
Option Explicit
Public Const OFS_MAXPATHNAME = 260
Public Const OF_READWRITE = &H2Public Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End TypePublic Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End TypePublic 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 TypePublic 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 TypePublic Declare Sub GetLocalTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME)Public Declare Function GetFileTime Lib "kernel32" _
(ByVal hFile As Long, lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As Long
Public Declare Function SetFileTime Lib "kernel32" _
(ByVal hFile As Long, _
lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As LongPublic Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIME, _
lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME, _
lpFileTime As FILETIME) As LongPublic Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, _
lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hFile As Long) As LongPublic Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End TypePublic Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400Public Declare Function ShellExecuteEx Lib "shell32" _
Alias "ShellExecuteEx" _
(SEI As SHELLEXECUTEINFO) As Long
Public Function GetFileDateString(CT As FILETIME) As String Dim ST As SYSTEMTIME
Dim ds As Single
'convert the passed FILETIME to a
'valid SYSTEMTIME format for display
If FileTimeToSystemTime(CT, ST) Then
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
GetFileDateString = Format$(ds, "DDDD MMMM D, YYYY")
Else: GetFileDateString = ""
End IfEnd Function
Public Function GetSystemDateString(ST As SYSTEMTIME) As String Dim ds As Single
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
If ds Then
GetSystemDateString = Format$(ds, "DDDD MMMM D, YYYY")
Else: GetSystemDateString = "error!"
End IfEnd Function
'--end block--''formOption ExplicitPrivate Sub Command2_Click() 'variables required
Dim hFile As Long
Dim fName As String
Dim tmp As String
'structures required
Dim OFS As OFSTRUCT
Dim SYS_TIME As SYSTEMTIME
Dim FT_CREATE As FILETIME
Dim FT_ACCESS As FILETIME
Dim FT_WRITE As FILETIME
Dim NEW_TIME As FILETIME
'assign the textbox entry to the filename
fName = (Text1)
'open the file
hFile = OpenFile(fName, OFS, OF_READWRITE)
'get the FILETIME info for the created,
'accessed and last write info
Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only ---------------------------
'show the system time info
tmp = "Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf
tmp = tmp & "Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf
tmp = tmp & "Last Modified:" & vbTab & GetFileDateString(FT_WRITE)
Text2.Text = tmp
'--------------------------------------------
'obtain the local system time
'(adjusts for the GMT deviation
'of the local time zone)
GetLocalTime SYS_TIME
'----- debug only ---------------------------
'show the system time info
tmp = ""
tmp = "Day:" & vbTab & SYS_TIME.wDay & vbCrLf
tmp = tmp & "Month:" & vbTab & SYS_TIME.wMonth & vbCrLf
tmp = tmp & "Year:" & vbTab & SYS_TIME.wYear & vbCrLf
tmp = tmp & "String:" & vbTab & GetSystemDateString(SYS_TIME)
Text3.Text = tmp
'--------------------------------------------
'convert the system time to a valid file time
Call SystemTimeToFileTime(SYS_TIME, NEW_TIME)
'set the created, accessed and modified dates all
'to the new dates. A null (0&) could be passed as
'any of the NEW_TIME parameters to leave that date unchanged.
Call SetFileTime(hFile, NEW_TIME, NEW_TIME, NEW_TIME)
're-read the updated FILETIME info for the created,
'accessed and last write info
Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only ---------------------------
'show the system time info
tmp = "New Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf
tmp = tmp & "New Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf
tmp = tmp & "New Last Modified:" & vbTab & GetFileDateString(FT_WRITE)
Text4.Text = tmp
'--------------------------------------------
'clean up by closing the file
Call CloseHandle(hFile)End Sub