作者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/m.aspx?n=bbs/Visual_Basic/M.1633624629.A.AF6.html
1F:→ niklee29: 我不確定你是不是要(設定檔案最後修改的時間)?是的 11/20 08:31
2F:→ niklee29: 有試試 File.SetLastWriteTime ? 11/20 08:31