首先用Windows API 函数CreateFile打开所想设置时间属性的文件(这里是mydoc.txt),返回值为改文件的句柄。 fchandle=CreateFile ("mydoc.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL); 接着我们用SetFileTime函数把该文件的时间属性修改一下就可以达到目的了。 SetFileTime的原型如下: BOOL SetFileTime( HANDLE hFile, // handle to the file CONST FILETIME *lpCreationTime, // time the file was created CONST FILETIME *lpLastAccessTime, // time the file was last // accessed CONST FILETIME *lpLastWriteTime // time the file was last written ); 其中的FILETIME结构定义如下: typedef struct _FILETIME { // ft DWORD dwLowDateTime; DWORD dwHighDateTime; } FILETIME; 使用起来并不方便,我们就先创建一个SYSTEMTIME结构的变量stime,再把它转换成FILETIME型 的。 SYSTEMTIME结构定义是这样的: typedef struct _SYSTEMTIME { // st WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME; 其中的数据成员的含义很容易理解,还是引用一下MSDN中的解释吧: wYear Specifies the current year. wMonth Specifies the current month; January = 1, February = 2, and so on. wDayOfWeek Specifies the current day of the week; Sunday = 0, Monday = 1, and so on. wDay Specifies the current day of the month. wHour Specifies the current hour. wMinute Specifies the current minute. wSecond Specifies the current second. wMilliseconds Specifies the current millisecond. 在定义了SYSTEMTIME型的变量stime之后,要向它的数据成员赋值,将时间设为我们想要的。这 里我就以我的出生时间为例: memset(&stime,0,sizeof(stime)); stime.wYear=1982; stime.wMonth=8; stime.wDay=12; stime.wDayOfWeek=4; stime.wHour=6; stime.wMinute=5; 第一句的作用是将其余不想对其赋值的成员全部置零。 接下来用一句 SystemTimeToFileTime(&stime,&ftime); 将SYSTEMTIME结构的变量stime,再把它转换成FILETIME型的ftime(ftime必须事先定义)便水 到渠成了: SetFileTime(fchandle,&ftime,NULL,&ftime); 这样就完成了文件时间属性的修改工作。
用文本方式将dsp文件打开,然后在里边加个空格后在保存,你看看时间是不是变了。:>
fchandle=CreateFile
("mydoc.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL
,NULL);
接着我们用SetFileTime函数把该文件的时间属性修改一下就可以达到目的了。
SetFileTime的原型如下:
BOOL SetFileTime(
HANDLE hFile, // handle to the file
CONST FILETIME *lpCreationTime, // time the file was created
CONST FILETIME *lpLastAccessTime, // time the file was last
// accessed
CONST FILETIME *lpLastWriteTime // time the file was last written
);
其中的FILETIME结构定义如下:
typedef struct _FILETIME { // ft
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
使用起来并不方便,我们就先创建一个SYSTEMTIME结构的变量stime,再把它转换成FILETIME型
的。
SYSTEMTIME结构定义是这样的:
typedef struct _SYSTEMTIME { // st
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
其中的数据成员的含义很容易理解,还是引用一下MSDN中的解释吧:
wYear
Specifies the current year.
wMonth
Specifies the current month; January = 1, February = 2, and so on.
wDayOfWeek
Specifies the current day of the week; Sunday = 0, Monday = 1, and so on.
wDay
Specifies the current day of the month.
wHour
Specifies the current hour.
wMinute
Specifies the current minute.
wSecond
Specifies the current second.
wMilliseconds
Specifies the current millisecond. 在定义了SYSTEMTIME型的变量stime之后,要向它的数据成员赋值,将时间设为我们想要的。这
里我就以我的出生时间为例:
memset(&stime,0,sizeof(stime));
stime.wYear=1982;
stime.wMonth=8;
stime.wDay=12;
stime.wDayOfWeek=4;
stime.wHour=6;
stime.wMinute=5;
第一句的作用是将其余不想对其赋值的成员全部置零。
接下来用一句
SystemTimeToFileTime(&stime,&ftime);
将SYSTEMTIME结构的变量stime,再把它转换成FILETIME型的ftime(ftime必须事先定义)便水
到渠成了:
SetFileTime(fchandle,&ftime,NULL,&ftime);
这样就完成了文件时间属性的修改工作。