[.NET]請問如何變更檔案的修改時間

看板Visual_Basic作者 (音太羽鈴)時間3年前 (2021/10/08 00:37), 編輯推噓0(002)
留言2則, 1人參與, 3年前最新討論串1/1
大家好 我目前想寫一個程式可以針對某個檔案修改成特定時間來作為辨識依據 目前查詢到網路上都是以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://www.ptt.cc/bbs/Visual_Basic/M.1633624629.A.AF6.html

11/20 08:31, 3年前 , 1F
我不確定你是不是要(設定檔案最後修改的時間)?是的
11/20 08:31, 1F

11/20 08:31, 3年前 , 2F
有試試 File.SetLastWriteTime ?
11/20 08:31, 2F
文章代碼(AID): #1XNo8rhs (Visual_Basic)
文章代碼(AID): #1XNo8rhs (Visual_Basic)