Newsgroup: it.comp.lang.visual-basic
Titolo: filedatetime

dovrei impostare la data ad un file (voglio decidere io la data di
creazione/modifica/accesso)
come fò ?
un metodo sarebbe cambiare la data del sistema con Date = xxxxx, ma non è
mica tanto carino...
c'è una api che si chiama SetFileDate, ma non so come usarla...
grazie!

 c'č una api che si chiama SetFileDate, ma non so come usarla...
La funzione č SetFileTime. Posto che non ci sia un metodo piů semplice (non
mi pare, a memoria, mentre in altri linguaggi la cosa č abbastanza piů
semplice) devi:
(a) aprire il file (via API)
(b) convertire le date (sono 3 in tutto, anche se solo 2 significative:
creazione, ultima scrittura, ultimo accesso) in altrettante strutture
FILETIME
(c) chiamare SetFileTime
(d) chiudere il file.
Si dovrebbe fare cosě (questo l'ho provato perché non č semplicissimo da
fare a memoria):
(NB: non tengo conto del fatto che il file possa essere lockato o di sola
lettura: in entrambi casi otterresti un errore, cioč FileDateTimeSet
ritornerebbe FALSE.)
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_NORMAL = &H80
Const INVALID_HANDLE_VALUE = -1&
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 Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As
Long) As Long
Private Declare Function CreateFile Lib "Kernel32.dll" 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 GetFileTime Lib "Kernel32.dll" (ByVal hFile As
Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME,
lpLastWriteTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32"
(lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function SetFileTime Lib "Kernel32.dll" (ByVal hFile As
Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME,
lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "Kernel32.dll"
(lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Function FileDateTimeSet(FileName As String, Optional CreationTime,
Optional LastAccessTime, Optional LastWriteTime) As Boolean
   On Error GoTo ErrHndl
   Dim hFile As Long
   Dim cTime As FILETIME, aTime As FILETIME, wTime As FILETIME, fTime As
FILETIME
   Dim msTime As SYSTEMTIME
   hFile = CreateFile(FileName, GENERIC_WRITE Or GENERIC_READ, _
      FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, _
      FILE_ATTRIBUTE_NORMAL, 0)
   If hFile = INVALID_HANDLE_VALUE Then
      GoTo ErrHndl
   End If
   If GetFileTime(hFile, cTime, aTime, wTime) = False Then GoTo ErrHndl
   If Not IsMissing(CreationTime) Then
      msTime = DateToSysTime(CDate(CreationTime))
      If SystemTimeToFileTime(msTime, fTime) = False Then GoTo ErrHndl
      If LocalFileTimeToFileTime(fTime, cTime) = False Then GoTo ErrHndl
   End If
   If Not IsMissing(LastAccessTime) Then
      msTime = DateToSysTime(CDate(LastAccessTime))
      If SystemTimeToFileTime(msTime, fTime) = False Then GoTo ErrHndl
      If LocalFileTimeToFileTime(fTime, aTime) = False Then GoTo ErrHndl
   End If
   If Not IsMissing(LastWriteTime) Then
      msTime = DateToSysTime(CDate(LastWriteTime))
      If SystemTimeToFileTime(msTime, fTime) = False Then GoTo ErrHndl
      If LocalFileTimeToFileTime(fTime, wTime) = False Then GoTo ErrHndl
   End If
   If SetFileTime(hFile, cTime, aTime, wTime) = False Then GoTo ErrHndl
   FileDateTimeSet = True
Quit:
   If hFile Then
      CloseHandle hFile
   End If
   Exit Function
ErrHndl:
   FileDateTimeSet = False
   GoTo Quit
End Function
Private Function DateToSysTime(dt As Date) As SYSTEMTIME
   Dim systime As SYSTEMTIME
   systime.wYear = Year(dt)
   systime.wMonth = Month(dt)
   systime.wDayOfWeek = Weekday(dt)
   systime.wDay = Day(dt)
   systime.wHour = Hour(dt)
   systime.wMinute = Minute(dt)
   systime.wSecond = Second(dt)
   systime.wMilliseconds = 0
   DateToSysTime = systime
End Function
----------------------------------------------------------------
Stefano Minardi
zet...@mclink.it
----------------------------------------------------------------

mazza, ma č semplicissimo!
grazie!