|
参阅了大量的源代码,加上自己的理解,才写出下面的代码,代码质量不高,拿出来让大家批评。
下面的设置文件时间的代码,花了我好长时间,我当时遇到了问题,在论坛求助,可是没人能够解决,我昏哦!今天我自己把问题解决了,这还要谢谢云舒给我的鼓励,非常感谢^_^
代码
//文件:SetFileTime.cpp
//时间:2006.7.13
//作者:猫降龙
//描述:可以设置文件属性里的时间,包括:创建时间、访问时间、修改时间
#include<stdio.h>
#include<windows.h>
int main(void)
{
SYSTEMTIME st; //系统时间
FILETIME ft_localTime;
FILETIME lpCreationTime; //创建时间
FILETIME lpLastAccessTime; //访问时间
FILETIME lpLastWriteTime; //修改时间
HANDLE hFile;
BOOL bResult;
hFile=CreateFile(\"mxlong.txt\",GENERIC_WRITE,0,
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
printf(\"Open File failed!\\nErrorCode:%d\\n\",GetLastError());
CloseHandle(hFile);
return 1;
}
GetLocalTime(&st);
printf(\"SystemTime:%04d-%02d-%02d %02d:%02d:%02d\\n\",
st.wYear ,st.wMonth ,st.wDay ,
st.wHour ,st.wMinute ,st.wSecond);
st.wYear=1982;
st.wMonth=7;
st.wDayOfWeek=1; //此处的值不要更改
st.wDay=4;
st.wHour=(21+8)%24;
st.wMinute=30;
st.wSecond=0;
st.wMilliseconds=0; //此处的值不要更改
SystemTimeToFileTime(&st,&ft_localTime);
printf(\"SystemTime:%04d-%02d-%02d %02d:%02d:%02d\\n\",
st.wYear ,st.wMonth ,st.wDay ,
st.wHour ,st.wMinute ,st.wSecond);
FileTimeToLocalFileTime(&ft_localTime,&lpCreationTime);
FileTimeToLocalFileTime(&ft_localTime,&lpLastAccessTime);
FileTimeToLocalFileTime(&ft_localTime,&lpLastWriteTime);
bResult=SetFileTime(hFile,&lpCreationTime,&lpLastAccessTime,&lpLastWriteTime);
if(bResult)
{
printf(\"File Time Set Succeed!\\n\");
CloseHandle(hFile);
return 0;
}
else
{
printf(\"File Time Set Failed!\\n%d\\n\",GetLastError());
CloseHandle(hFile);
return 1;
}
CloseHandle(hFile);
return 0;
getchar();
}
读取文件时间代码
代码
//文件:GetFileTime.cpp
//时间:2006.7.13
//作者:猫降龙
//描述:可以读取文件属性里的时间,包括:创建时间、访问时间、修改时间
#include<stdio.h>
#include<windows.h>
int main(void)
{
SYSTEMTIME st_systemTime;
FILETIME ft_localTime;
FILETIME lpCreationTime;
FILETIME lpLastAccessTime;
FILETIME lpLastWriteTime;
HANDLE hFile;
long retval;
hFile=CreateFile(\"mxlong.txt\",GENERIC_READ,0,
0,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,0);
if(hFile==INVALID_HANDLE_VALUE)
{
printf(\"Open File failed!\\nErrorCode:%d\\n\",GetLastError());
CloseHandle(hFile);
return 1;
}
retval=GetFileTime(hFile,&lpCreationTime,&lpLastAccessTime,&lpLastWriteTime);
if(retval)
{
//CreationTime(创建时间)
FileTimeToLocalFileTime(&lpCreationTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);
printf(\"CreationTime:%04d-%02d-%02d %02d:%02d:%02d\\n\",
st_systemTime.wYear ,st_systemTime.wMonth ,st_systemTime.wDay ,
st_systemTime.wHour ,st_systemTime.wMinute ,st_systemTime.wSecond);
//LastWriteTime(修改时间)
FileTimeToLocalFileTime(&lpLastWriteTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);
printf(\"LastWriteTime:%04d-%02d-%02d %02d:%02d:%02d\\n\",
st_systemTime.wYear ,st_systemTime.wMonth ,st_systemTime.wDay ,
st_systemTime.wHour ,st_systemTime.wMinute ,st_systemTime.wSecond);
//LastAccessTime(访问时间)
FileTimeToLocalFileTime(&lpLastAccessTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);
printf(\"LastAccessTime:%04d-%02d-%02d %02d:%02d:%02d\\n\",
st_systemTime.wYear ,st_systemTime.wMonth ,st_systemTime.wDay ,
st_systemTime.wHour ,st_systemTime.wMinute ,st_systemTime.wSecond);
CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
return 0;
getchar();
} |
|