怎样读取文件的创建日期阿?

wdshyx 2003-11-28 02:52:26

HANDLE hExe = CreateFile ("E:\a.doc", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FILETIME lpCreationTime; // 文件夹的创建时间
FILETIME lpLastAccessTime; // 对文件夹的最近访问时间
FILETIME lpLastWriteTime; // 文件夹的最近修改时间
// 获取文件夹时间属性信息
GetFileTime(hExe, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime);
FILETIME ftime;
SYSTEMTIME stime;
FileTimeToLocalFileTime(&lpLastAccessTime, &ftime); // 转换成本地时间
FileTimeToSystemTime(&ftime, &stime); // 转换成系统时间格式
TDateTime crtTime = SystemTimeToDateTime(stime);
Edit1->Text = FormatDateTime("yyyy/mm/dd", crtTime);
CloseHandle(hExe); // 关闭打开过的文件夹


不管是那个文件,也不管是创建时间、最近访问时间还是最近一次修改时间,最后都显示“1601-01-01”
...全文
223 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
heng2003 2003-11-28
  • 打赏
  • 举报
回复
算了,好人做到底,给你一个我写的取文件时间的函数吧

bool ExtractFileTime(char* FileName,TDateTime& CreateTime,TDateTime& AccessTime,TDateTime& WriteTime)
{
if(!FileName || FileName[0]=='\0'){
return false;
}
HANDLE hfile = CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
if(hfile!=INVALID_HANDLE_VALUE){
FILETIME ftcreate,ftwrite,ftaccess;
if(GetFileTime(hfile,&ftcreate,&ftaccess,&ftwrite)){
SYSTEMTIME stcreate,stwrite,staccess;

FileTimeToLocalFileTime(&ftcreate,&ftcreate);
FileTimeToLocalFileTime(&ftaccess,&ftaccess);
FileTimeToLocalFileTime(&ftwrite,&ftwrite);

FileTimeToSystemTime(&ftcreate,&stcreate);
FileTimeToSystemTime(&ftaccess,&staccess);
FileTimeToSystemTime(&ftwrite,&stwrite);

CreateTime = SystemTimeToDateTime(stcreate);
AccessTime = SystemTimeToDateTime(staccess);
WriteTime = SystemTimeToDateTime(stwrite);

CloseHandle(hfile);

return true;
}else{
CloseHandle(hfile);
return false;
}
}else{
return false;
}
}
heng2003 2003-11-28
  • 打赏
  • 举报
回复
大概的看了一下
从取完文件句柄之后得到文件时间的代码没问题
问题应该就是你取句柄失败,使用非法文件句柄取时间了,导致取文件时间出现错误
heng2003 2003-11-28
  • 打赏
  • 举报
回复
引用楼主的源程序:
HANDLE hExe = CreateFile ("E:\a.doc", GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

拜托,请使用"e:\\a.doc"
另外调用了CreateFile 来获取文件句柄
请保持一个好习惯那就是检查一下获取句柄是否成功
判断一下hExe 是否为 非 INVALID_HANDLE_VALUE 值
其他的我还没来得及看
Chimae 2003-11-28
  • 打赏
  • 举报
回复
TDateTime __fastcall TfrmContract::GetFileTime(String tmpFile, int intType)
{
WIN32_FIND_DATA ffd;
WORD fd, ft;
DWORD fdt;
FILETIME lft, Time;
HANDLE H;
H = FindFirstFile(tmpFile.c_str(), &ffd);
switch (intType)
{
case 0:
Time = ffd.ftCreationTime;//文件创建日期
break;
case 1:
Time = ffd.ftLastWriteTime;//最后修改日期
break;
case 2:
Time = ffd.ftLastAccessTime;//最后访问日期
break;
default:
Time = ffd.ftCreationTime;
}
if (H!=INVALID_HANDLE_VALUE)
{
FindClose(H);
FileTimeToLocalFileTime(&Time, &lft);
FileTimeToDosDateTime(&lft, &fd, &ft);
return FileDateToDateTime(MAKELONG(ft, fd));
}
else
return TDateTime();
}
  • 打赏
  • 举报
回复
http://www.cppfans.com/forum/guestbkans.asp?pn=1&id=436
albeta 2003-11-28
  • 打赏
  • 举报
回复
用FindFirst看看?
wdshyx 2003-11-28
  • 打赏
  • 举报
回复
其他文件我也试过了,就是读取文件出错!他们都没被使用的!
lanren_me 2003-11-28
  • 打赏
  • 举报
回复
看看你那个a.exe是不是正在被别的文件使用;

试试独占方式!
wdshyx 2003-11-28
  • 打赏
  • 举报
回复
gycxy(静水)
您提出的那个解决方案应该是不行的!因为它同我那个没什么区别。关键是它转化为系统时间的时候就已经是那个日期了,而不是我转化为字符串的时候出的问题!
wdshyx 2003-11-28
  • 打赏
  • 举报
回复
FileOpen("a.exe",fmOpenRead);我读出来值总是-1,也就是说没成功阿!
gycxy 2003-11-28
  • 打赏
  • 举报
回复
crtTime.FileDateToDateTime(ftime);
Edit1->Text = crtTime.DateString();
就可以了,
MEFULEU 2003-11-28
  • 打赏
  • 举报
回复
typedef struct _FINFO //导入的文件结构信息
{
String FileName; //文件名称;
String FileSize; //文件大小;
String FileType; //文件类型;
String FileTime; //文件修改时间;

} TFINFO ;


void GetFileInFo(String FileName, //存放当前目录所有文件的列表;
TFINFO &info //返回结果的结构数组
)
{
/*
FileGetAttr()
  原型:extern PACKAGE int __fastcall FileGetAttr(const AnsiString FileName);

  功能:取得文件属性,如果出错返回-1

返回值如下表,如果返回$00000006表示是一个具有隐含和系统属性的文件(4+2)

常量 值 含义
faReadOnly $00000001 只读文件
faHidden $00000002 隐含文件
faSysFile $00000004 系统文件
faVolumeID $00000008 卷标
faDirectory $00000010 目录
faArchive $00000020 归档文件

  例:if(FileGetAttr("LLL.TXT")&0x2)ShowMessage("这是一个有隐含属性的文件");
  与此对应的有FileSetAttr() ,请自已查阅帮助系统

*/
info.FileName="";
info.FileSize=0;
info.FileType="";
info.FileTime=0;

//获取文件的名称
info.FileName=ExtractFileName(FileName);
//获取文件属性
int lk=FileGetAttr(FileName);

if (lk&faReadOnly) info.FileType="只读文件";
else if(lk&faHidden) info.FileType="隐藏文件";
else if(lk&faSysFile) info.FileType="系统文件";
else if(lk&faVolumeID) info.FileType="卷标";
else if(lk&faDirectory) info.FileType="目录";
else if(lk&faArchive) info.FileType="归档文件";
else info.FileType="隐藏的系统文件";

//获取文件的大小;
int fhwnd;
int td;
fhwnd=FileOpen(FileName,fmOpenRead);
td=FileGetDate(fhwnd); //获取时间;
lk=FileSeek(fhwnd,0,2); //移动到文件尾;
FileClose(fhwnd);

info.FileSize=FloatToStr(lk/1024)+"k";

info.FileTime=FileDateToDateTime(td);





}

13,874

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧