作者NTBR (音太羽铃)
看板Visual_Basic
标题[.NET]请问如何变更档案的修改时间
时间Fri Oct 8 00:37:07 2021
大家好
我目前想写一个程式可以针对某个档案修改成特定时间来作为辨识依据
目前查询到网路上都是以API来达成这个功能
参考了许多写法之後最後虽然执行没出现错误警告
但是档案的日期状态一样没有变化(功力不足看不出问题)
想请帮忙看这段语法是哪边有问题
作业环境是Windows10
宣告:
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Structure FILETIME
Dim dwLowDateTime As Long
Dim dwHighDateTime As Long
End Structure
Structure SYSTEMTIME
Dim wYear As Integer
Dim wMonth As Integer
Dim wDayOfWeek As Integer
Dim wDay As Integer
Dim wHour As Integer
Dim wMinute As Integer
Dim wSecond As Integer
Dim wMilliseconds As Long
End Structure
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 Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim my_Date As Date, lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
my_Date = "2001/01/01 12:34:56"
udtSystemTime.wYear = DateAndTime.Year(my_Date)
udtSystemTime.wMonth = DateAndTime.Month(my_Date)
udtSystemTime.wDay = DateAndTime.Day(my_Date)
udtSystemTime.wDayOfWeek = DateAndTime.Weekday(my_Date) - 1
udtSystemTime.wHour = DateAndTime.Hour(my_Date)
udtSystemTime.wMinute = DateAndTime.Minute(my_Date)
udtSystemTime.wSecond = DateAndTime.Second(my_Date)
udtSystemTime.wMilliseconds = 0
SystemTimeToFileTime(udtSystemTime, udtLocalTime)
LocalFileTimeToFileTime(udtLocalTime, udtFileTime)
lngHandle = CreateFile(Application.StartupPath & "\AAA.txt",_
GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&,_
OPEN_EXISTING, 0, 0)
SetFileTime(lngHandle, udtFileTime, udtFileTime, udtFileTime)
CloseHandle(lngHandle)
End Sub
万分感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.128.240.200 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1633624629.A.AF6.html
1F:→ niklee29: 我不确定你是不是要(设定档案最後修改的时间)?是的 11/20 08:31
2F:→ niklee29: 有试试 File.SetLastWriteTime ? 11/20 08:31