如何使用api得到文件属性中的日期 菜菜地问 20块钱

woshidabendan 2004-08-11 09:00:17
如题 呵呵 请高手赐教
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gujianda 2004-08-11
  • 打赏
  • 举报
回复
呵呵,漏了自定义类型:

'文件操作部分
Public Type MyFT
YY As String * 4
MM As String * 2
DD As String * 2
HH As String * 2
NN As String * 2
SS As String * 2
End Type

Public FT As MyFT
Gujianda 2004-08-11
  • 打赏
  • 举报
回复
我是这样做的:


'===========================================
'以下数据类型应用于 GetFileTime 函数

Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

'===================================================
'以下数据类型应用于 FileTimeToSystemTime 函数
Public 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

Public Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

'自定义函数:
Function Get_FileTimeInfo(ByVal hFile As Long, FT As MyFT) As Long
On Error GoTo err_sit
Dim req As Long
Dim ctFTIME As FILETIME, laFTIME As FILETIME, lwFTIME As FILETIME
Dim cTIME As SYSTEMTIME, aTIME As SYSTEMTIME, wTIME As SYSTEMTIME
req = GetFileTime(hFile, ctFTIME, laFTIME, lwFTIME)
req = FileTimeToSystemTime(ctFTIME, cTIME)
req = FileTimeToSystemTime(laFTIME, aTIME)
req = FileTimeToSystemTime(lwFTIME, wTIME)

FT.YY = Format(wTIME.wYear, "0000")
FT.MM = Format(wTIME.wMonth, "00")
FT.DD = Format(wTIME.wDay, "00")
FT.HH = Format(wTIME.wHour, "00")
FT.NN = Format(wTIME.wMinute, "00")
FT.SS = Format(wTIME.wSecond, "00")

err_sit:
Get_FileTimeInfo = req
End Function

SafeF8 2004-08-11
  • 打赏
  • 举报
回复
这个例子可以得到文件的创建日期、大小、等属性
'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()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
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
of123 2004-08-11
  • 打赏
  • 举报
回复
不用 API 也可以:

下面的代码用一个文件举例说明了 DateCreated、DateLastAccessed 属性的用法:

Sub ShowFileAccessInfo(filespec)
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = UCase(filespec) & vbCrLf
s = s & "Created: " & f.DateCreated & vbCrLf
s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
MsgBox s, 0, "File Access Info"
End Sub


woshidabendan 2004-08-11
  • 打赏
  • 举报
回复
lxxldd师兄 说得清楚一点好吗
lxxldd 2004-08-11
  • 打赏
  • 举报
回复
用fso就可以
woshidabendan 2004-08-11
  • 打赏
  • 举报
回复
等我看看 呵呵 谢谢大虾
TechnoFantasy 2004-08-11
  • 打赏
  • 举报
回复
这个程序演示了如何不使用API函数而是利用Microsoft Runtime Scripting Library实现获得文件的属性(包括文件大小、类型、文件建立以及访问日期等信息,类似于在Explore窗口中在文件中单击右键然后在菜单中选“属性”项获得文件信息的功能)

http://www.moon-soft.com/download/other/fsofileinfo.ZIP

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧