得到文件日期和时间的方法很多,其中标准 C 函数 VCL 函数都比较简单,API 函数最复杂,但是可以得到更多的文件参数
void __fastcall TForm1::Button5Click(TObject *Sender)
{
//-- 标准 C 语言函数 --
FILE *f;
if((f=fopen("MyData.dat","rb"))!=NULL)
{
ftime t;
getftime(fileno(f),&t);
TDateTime dt = EncodeDateTime(t.ft_year+1980,t.ft_month,t.ft_day,t.ft_hour,t.ft_min,t.ft_tsec*2,0);
Edit1->Text = FormatDateTime("yyyy-mm-dd hh:nn:ss",dt);
fclose(f);
}
//-- 在 BCB 里面用 Delphi/VCL 函数
int hVclFile;
if((hVclFile=FileOpen("MyData.dat",fmOpenRead))!=-1)
{
TDateTime t = FileDateToDateTime(FileGetDate(hVclFile));
Edit2->Text = FormatDateTime("yyyy-mm-dd hh:nn:ss",t);
FileClose(hVclFile);
}
//-- 用 Windows API 函数
HANDLE hFile;
if((hFile=CreateFile("MyData.dat", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))!=INVALID_HANDLE_VALUE)
{
BY_HANDLE_FILE_INFORMATION fi;
TIME_ZONE_INFORMATION tz;
GetFileInformationByHandle(hFile,&fi);
GetTimeZoneInformation(&tz);
TDateTime dt = (*(__int64*)&fi.ftLastWriteTime)/(double)(24*60*60*10000000.0) + EncodeDateTime(1601,1,1,0,0,0,0) - tz.Bias/(60.0*24.0);
Edit3->Text = FormatDateTime("yyyy-mm-dd hh:nn:ss", dt);
CloseHandle(hFile);
}
}
--------------------next---------------------
#include
#include
#include
void __fastcall TForm1::Button6Click(TObject *Sender)
{
int hFind;
_finddatai64_t fd;
if((hFind=_findfirsti64("MyData.dat", &fd))!=-1)
{
struct tm *t;
t = localtime(&fd.time_create);
Edit1->Text=AnsiString().sprintf("创建 %04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
t = localtime(&fd.time_write );
Edit2->Text=AnsiString().sprintf("修改 %04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
t = localtime(&fd.time_access);
Edit3->Text=AnsiString().sprintf("访问 %04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
Edit4->Text = "长度 " + AnsiString(fd.size) + " byte(s)";
Edit5->Text = "属性 " + AnsiString((fd.attrib&FA_RDONLY)?"R":"-") + AnsiString((fd.attrib&FA_ARCH)?"A":"-") + AnsiString((fd.attrib&FA_HIDDEN)?"H":"-") + AnsiString((fd.attrib&FA_SYSTEM)?"S":"-");
_findclose(hFind);
}
}
--------------------next---------------------
阅读(1149) | 评论(0) | 转发(0) |