急要一段vb修改文件属性时间的代码
在text1,text2,text3三个文本框中分别输入年,月,日后点击按钮修改文件属性的年月日。感谢各位大大了...
在text1,text2,text3三个文本框中分别输入年,月,日后点击按钮修改文件属性的年月日。感谢各位大大了
展开
2个回答
展开全部
要三个文本框,1个按钮,1个CommonDialog控件:
CommonDialog控件通过,[工程]--[部件],勾选:Microsoft Common Dialog Control 6.0 (SP6)来添加到工具箱:
代码如下:
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
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 Sub Command1_Click()
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim d_Date As String
d_Date = Text1.Text & "-" & Text2.Text & "-" & Text3.Text
m_Date = Format(d_Date, "YYYY-MM-DD")
CommonDialog1.DialogTitle = "Choose a file ..."
CommonDialog1.Filter = "All Files (*.*)|*.*"
CommonDialog1.ShowOpen
udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = Weekday(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wMinute = Minute(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CommonDialog1.FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CommonDialog1.FileName + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
复制以上代码,OK。
CommonDialog控件通过,[工程]--[部件],勾选:Microsoft Common Dialog Control 6.0 (SP6)来添加到工具箱:
代码如下:
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
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 Sub Command1_Click()
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim d_Date As String
d_Date = Text1.Text & "-" & Text2.Text & "-" & Text3.Text
m_Date = Format(d_Date, "YYYY-MM-DD")
CommonDialog1.DialogTitle = "Choose a file ..."
CommonDialog1.Filter = "All Files (*.*)|*.*"
CommonDialog1.ShowOpen
udtSystemTime.wYear = Year(m_Date)
udtSystemTime.wMonth = Month(m_Date)
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = Weekday(m_Date) - 1
udtSystemTime.wHour = Hour(m_Date)
udtSystemTime.wMinute = Minute(m_Date)
udtSystemTime.wSecond = Second(m_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CommonDialog1.FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CommonDialog1.FileName + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
复制以上代码,OK。
追问
您好,想问一下怎样通过拖拽方式修改时间。
1、添加类似例表的东西或者拖拽到Form1点击按钮直接修改。
2、增加text4、text5、text6,怎样添加时分秒进去。
感谢您了!老师
追答
你好,要添加时,分,秒,要读一下原来的Command1_Click()里的代码,应该可以读懂:
修改如下,我已经适当的加了中文注释;
Private Sub Command1_Click()
Dim m_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
Dim d_Date As String
'为了增加时、分、秒,使用的变量
Dim h_Date As Date
d_Date = Text1.Text & "-" & Text2.Text & "-" & Text3.Text
m_Date = Format(d_Date, "YYYY-MM-DD")
'用Text4、Text5、Text6接收时分秒
h_Date = Format(Text4.Text & ":" & Text5.Text & ":" & Text6.Text, "HH:MM:SS")
CommonDialog1.DialogTitle = "Choose a file ..."
CommonDialog1.Filter = "All Files (*.*)|*.*"
CommonDialog1.ShowOpen
'设置年
udtSystemTime.wYear = Year(m_Date)
'设置月
udtSystemTime.wMonth = Month(m_Date)
'设置日
udtSystemTime.wDay = Day(m_Date)
udtSystemTime.wDayOfWeek = Weekday(m_Date) - 1
'设置时
udtSystemTime.wHour = Hour(h_Date)
'设置分
udtSystemTime.wMinute = Minute(h_Date)
'设置秒
udtSystemTime.wSecond = Second(h_Date)
udtSystemTime.wMilliseconds = 0
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(CommonDialog1.FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
MsgBox "The date of the file '" + CommonDialog1.FileName + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
End Sub
另外,要拖曳文件达到此功能,我记得最近2个月里,我在这里回答过这个问题,请你搜一搜.
另外,我想说,你可以安装VB6的MSDN,获得即时帮助,并有助于编程能力的提高.
展开全部
看参考
参考资料: http://hi.baidu.com/52malt/blog/item/594f935826f6ae80810a187d.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询