急,如何获得一个文件夹的创建日期?(200分)

  • 主题发起人 主题发起人 thudb
  • 开始时间 开始时间
你可以使用CreateFile打开一个文件夹,得到它的句柄,<br>/***********************************<br>The CreateFile function creates or opens the following objects<br>and returns a handle that can be used to access the object: <br>&nbsp; * Consoles <br>&nbsp; * Communications resources <br>&nbsp; * Directories (open only) <br>&nbsp; &nbsp; ~~~~~~~~~~~~~~~~~~~~~~~~~~<br>&nbsp; * Disk devices (Windows NT/2000 only) <br>&nbsp; * Files <br>&nbsp; * Mailslots <br>&nbsp; * Pipes <br>****************************************/<br>然后使用下面函数来得到你要的时间<br>BOOL GetFileTime(<br>&nbsp; HANDLE hFile, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle to file<br>&nbsp; LPFILETIME lpCreationTime, &nbsp; &nbsp;// creation time<br>&nbsp; LPFILETIME lpLastAccessTime, &nbsp;// last access time<br>&nbsp; LPFILETIME lpLastWriteTime &nbsp; &nbsp;// last write time<br>);<br>
 
GetFileInformation
 
&nbsp;TDosTime=Record<br>&nbsp; &nbsp; Hour &nbsp; &nbsp; &nbsp;: Byte;<br>&nbsp; &nbsp; Minutes &nbsp; : Byte;<br>&nbsp; &nbsp; seconds &nbsp; : Byte;<br>&nbsp; end;<br><br>&nbsp; TDosDate = Record<br>&nbsp; &nbsp; Year &nbsp; : Word;<br>&nbsp; &nbsp; Month &nbsp;: Byte;<br>&nbsp; &nbsp; Day &nbsp; &nbsp;: Byte;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hFile: THandle; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// a handle to the opened file<br><br>implementation<br><br>{this function provides a convenient way to convert a<br><br>&nbsp;Dos time into its component parts}<br>function ConvertDosTimeToSystemTime(FileDosTime: WORD): TDosTime;<br>var<br>&nbsp; DosTime: TDosTime;<br>begin<br>&nbsp; DosTime.Seconds := (FileDosTime and $1F) * 2;<br>&nbsp; DosTime.Minutes := (FileDosTime and $7E0) shr 5;<br>&nbsp; DosTime.Hour &nbsp; &nbsp;:= (FileDosTime and $F800) shr 11;<br>&nbsp; Result &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= DosTime;<br>end;<br><br>{this function provides a convenient way to convert a<br>&nbsp;Dos date into its component parts}<br><br>function ConvertDosDateToSystemDate(FileDosDate: WORD): TDosDate;<br>var<br>&nbsp; DosDate: TDosDate;<br>begin<br>&nbsp; DosDate.Day &nbsp; := FileDosDate and $1F;<br>&nbsp; DosDate.Month := FileDosDate and $1E0 shr 5;<br>&nbsp; DosDate.Year &nbsp;:= (FileDosDate and $FE00) shr 9 + 1980;<br>&nbsp; Result &nbsp; &nbsp; &nbsp; &nbsp;:= DosDate;<br>end;<br><br>procedure TForm1.SpeedButton2Click(Sender: TObject);<br>var<br>&nbsp; Security: TSecurityAttributes; &nbsp; &nbsp; &nbsp; &nbsp;// attributes for the opened file<br><br>&nbsp; FileName: PChar; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds the filename<br>&nbsp; WriteTime, LocalTime: TFILETIME; &nbsp; &nbsp; &nbsp;// holds file times<br>&nbsp; DosDate, DosTime: WORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds the Dos date and time<br>&nbsp; infoDosTime: TDosTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds Dos time information<br>&nbsp; infoDosDate: TDosDate; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds Dos date information<br>&nbsp; SystemTime: TSystemTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds the last modification time<br><br>begin<br>&nbsp; {setup the security attributes for the opened file}<br>&nbsp; Security.nLength := SizeOf(TSecurityAttributes);<br>&nbsp; Security.lpSecurityDescriptor := nil;<br>&nbsp; Security.bInheritHandle := FALSE;<br><br>&nbsp; {display the open dialog box}<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; {display the selected filename...}<br>&nbsp; &nbsp; FileName := PChar(OpenDialog1.FileName);<br>&nbsp; &nbsp; StatusBar1.SimpleText := FileName;<br><br>&nbsp; &nbsp; {...and open it}<br><br>&nbsp; &nbsp; hFile := CreateFile(PChar(FileName),GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_SHARE_READ or FILE_SHARE_WRITE, @Security,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);<br><br>&nbsp; &nbsp; {if there was an error, show a message}<br>&nbsp; &nbsp; if hFile = INVALID_HANDLE_VALUE then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ShowMessage('Error Opening File');<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp; {retrieve the last modification time}<br><br>&nbsp; GetFileTime(hFile, nil, nil, @WriteTime);<br><br>&nbsp; {convert the time to local file time}<br>&nbsp; FileTimeToLocalFileTime(WriteTime, LocalTime);<br><br>&nbsp; {finally, convert the time to the system time, so that it<br>&nbsp; &nbsp;will match the file time displayed in the Explorer}<br>&nbsp; FileTimeToSystemTime(LocalTime, SystemTime);<br><br>&nbsp; {convert the file time into Dos date and time components...}<br>&nbsp; FileTimeToDosDateTime(LocalTime, DosDate, DosTime);<br><br>&nbsp; {...and convert it back}<br><br>&nbsp; if not DosDateTimeToFileTime(DosDate, DosTime, LocalTime) then<br>&nbsp; &nbsp; ShowMessage ('An error occured when converting Dos date and time back to'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;' file time.');<br><br>&nbsp; {break out the component parts of the Dos date and time for easy display}<br>&nbsp; infoDosTime := ConvertDosTimeToSystemTime(DosTime);<br>&nbsp; infoDosDate := ConvertDosDateToSystemDate(DosDate);<br><br>&nbsp; with infoDosTime do<br>&nbsp; &nbsp; Edit1.Text := ComboBox1.Items[infoDosDate.Month - 1]+ ' ' +<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(infoDosDate.Day) + ',' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(infoDosDate.Year) + ' &nbsp;' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Hour) + ':' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Minutes) + ':' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntToStr(Seconds) ;<br><br>&nbsp; {indicate the time of day}<br>&nbsp; case SystemTime.WHour of<br>&nbsp; &nbsp; 12 &nbsp; &nbsp; &nbsp; &nbsp;: Label1.Caption := 'PM';<br>&nbsp; &nbsp; 13..24 &nbsp; &nbsp;: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label1.Caption := 'PM';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemTime.wHour:=SystemTime.wHour - 12;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; : SystemTime.wHour:= 12;<br>&nbsp; else<br>&nbsp; &nbsp; Label1.Caption := 'AM';<br>&nbsp; end;<br><br>&nbsp; {display the last modification time of the file}<br>&nbsp; SpinEdit1.Value &nbsp; &nbsp; := SystemTime.wYear;<br>&nbsp; SpinEdit2.Value &nbsp; &nbsp; := SystemTime.wHour;<br>&nbsp; SpinEdit3.Value &nbsp; &nbsp; := SystemTime.wMinute;<br>&nbsp; SpinEdit4.Value &nbsp; &nbsp; := SystemTime.wSecond;<br>&nbsp; ComboBox1.ItemIndex := SystemTime.wMonth - 1;<br>&nbsp; Calendar1.Month &nbsp; &nbsp; := SystemTime.wMonth;<br>&nbsp; Calendar1.Day &nbsp; &nbsp; &nbsp; := SystemTime.wDay;<br><br>end;<br><br>procedure TForm1.SpeedButton3Click(Sender: TObject);<br>var<br>&nbsp; FileTime, LocalFileTime: TFileTime; &nbsp;// holds file times<br>&nbsp; SystemTime: TSystemTime; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds system time information<br>begin<br>&nbsp; {prepare the time information from the values set by the user}<br>&nbsp; SystemTime.wHour &nbsp; := SpinEdit2.Value;<br><br>&nbsp; if (Label1.Caption = 'PM') and (SystemTime.wHour &lt; 12) then<br>&nbsp; &nbsp; &nbsp; SystemTime.wHour := SystemTime.wHour + 12;<br><br>&nbsp; SystemTime.wMinute := SpinEdit3.Value;<br>&nbsp; SystemTime.wSecond := SpinEdit4.Value;<br>&nbsp; SystemTime.wYear &nbsp; := SpinEdit1.Value;<br>&nbsp; SystemTime.wMonth &nbsp;:= ComboBox1.ItemIndex + 1;<br>&nbsp; SystemTime.wDay &nbsp; &nbsp;:= Calendar1.Day;<br><br>&nbsp; {convert the system time to a local file time}<br>&nbsp; SystemTimeToFileTime(SystemTime,LocalFileTime);<br><br>&nbsp; {convert the local file time to a file time that the file system understands}<br>&nbsp; LocalFileTimeToFileTime(LocalFileTime,FileTime);<br><br>&nbsp; {use this time to set the last modification time which shows<br>&nbsp; &nbsp;up in the Explorer}<br>&nbsp; SetFileTime(hFile, nil, nil, @FileTime);<br>end;<br>
 
jyniu、guotong<br>好象CreateFile只能打开文件,而不能打开文件夹,我到的是哪错了?
 
搞定,哈哈<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; SR:TSearchRec;<br>&nbsp; sResult:Integer;<br>begin<br>sResult:=FindFirst('c:/temp',faDirectory,SR);<br>if sResult=0 then<br>&nbsp; ShowMessage(DateTimeToStr(FileDateToDateTime(SR.Time)));<br>SysUtils.FindClose(SR);<br>end;<br>
 
教父的答案最简洁。<br>guotong、jyniu辛苦了,用CreateFile打开文件夹, dwFlagsAndAttributes 参数应为FILE_FLAG_BACKUP_SEMANTICS。<br>
 
后退
顶部