VC MFC 修改目录时间方法
2007-12-22 13:56
302 查看
// 修改指定目录的时间
BOOL SetDirTime(char DirName, SYSTEMTIME new_stime)
{
HANDLE hDir;
// 打开目录的Win32 API调用
// 必须“写”方式打开
// 打开现存的目录
// 只有这样才能打开目录
hDir = CreateFile ( DirName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_DELETE,NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir ==INVALID_HANDLE_VALUE ) return FALSE;
// 打开失败时返回
FILETIME lpCreationTime;
// creation time目录的创建时间
FILETIME lpLastAccessTime;
// last access time最近一次访问目录的时间
FILETIME lpLastWriteTime;
// last write time最近一次修改目录的时间
SystemTimeToFileTime(&new_stime, &lpCreationTime);
// 转换成文件的时间格式
SystemTimeToFileTime(&new_stime, &lpLastAccessTime);
SystemTimeToFileTime(&new_stime, &lpLastWriteTime);
// 修改目录时间的Win32 API函数调用
BOOL retval = SetFileTime( hDir, &lpCreationTime,&lpLastAccessTime,&lpLastWriteTime);
CloseHandle(hDir);
// 关闭目录
return retval;
// 返回修改成功与否的返回码
}
// 获取指定目录的时间
BOOL GetDirTime(char DirName, SYSTEMTIME & stime)
{
HANDLE hDir;
// 打开目录的Win32 API调用
// 只需读方式打开即可
// 打开现存的目录
hDir = CreateFile ( DirName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,NULL);
FILETIME lpCreationTime;
// creation time目录创建时间
FILETIME lpLastAccessTime;
// last access time目录最近访问时间
FILETIME lpLastWriteTime;
// last write time目录最近修改时间
// 获取目录日期和时间的Win32 API调用
BOOL retval = GetFileTime( hDir,&lpCreationTime,&lpLastAccessTime &lpLastWriteTime);
if ( retval )
{
FILETIME ftime;
FileTimeToLocalFileTime(&lpLastWriteTime, &ftime);
// 转换成本地时间
FileTimeToSystemTime(&ftime, &stime) ;
// 转换成系统时间格式}
CloseHandle(hDir);
return retval;
}
}
int DoTest(char DirName)
{
SYSTEMTIME stime;
printf(″testing for directory [%s]/n″, DirName) ;
// 显示修改前目录的时间
if ( GetDirTime(DirName, stime) )
printf(″before change is %04d-%02d-%02d %02d:%02d:%02d/n″, stime.wYear , stime.wMonth , stime.wDay , stime.wHour , stime.wMinute, stime.wSecond );
else
printf(″failed to get the datetime of directory.../n″);
stime.wYear = 1995;
stime.wMonth = 5;
stime.wDay = 12;
stime.wHour = 10 - 8;
// GMT time, GMT+8 for China PRC
stime.wMinute = 11;
stime.wSecond = 12;
// GetSystemTime(&stime);
// 如果要设置成当前的时间
// 修改目录的时间
if ( SetDirTime(DirName, stime) )
printf(″success to change datetime of directory./n″);
else
printf(″failed to change the datetime of directory.../n″);
// 显示修改后目录的时间
if ( GetDirTime(DirName, stime) )
printf(″after change is %04d-%02d-%02d %02d:%02d:%02d/n″,stime.wYear , stime.wMonth , stime.wDay ,stime.wHour , stime.wMinute, stime.wSecond );
else
printf(″failed to get the datetime of directory.../n″);
return 0;
}
void main(int argc, char argv[])
{
DoTest(″c://dir″);
}
BOOL SetDirTime(char DirName, SYSTEMTIME new_stime)
{
HANDLE hDir;
// 打开目录的Win32 API调用
// 必须“写”方式打开
// 打开现存的目录
// 只有这样才能打开目录
hDir = CreateFile ( DirName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_DELETE,NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir ==INVALID_HANDLE_VALUE ) return FALSE;
// 打开失败时返回
FILETIME lpCreationTime;
// creation time目录的创建时间
FILETIME lpLastAccessTime;
// last access time最近一次访问目录的时间
FILETIME lpLastWriteTime;
// last write time最近一次修改目录的时间
SystemTimeToFileTime(&new_stime, &lpCreationTime);
// 转换成文件的时间格式
SystemTimeToFileTime(&new_stime, &lpLastAccessTime);
SystemTimeToFileTime(&new_stime, &lpLastWriteTime);
// 修改目录时间的Win32 API函数调用
BOOL retval = SetFileTime( hDir, &lpCreationTime,&lpLastAccessTime,&lpLastWriteTime);
CloseHandle(hDir);
// 关闭目录
return retval;
// 返回修改成功与否的返回码
}
// 获取指定目录的时间
BOOL GetDirTime(char DirName, SYSTEMTIME & stime)
{
HANDLE hDir;
// 打开目录的Win32 API调用
// 只需读方式打开即可
// 打开现存的目录
hDir = CreateFile ( DirName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,NULL);
FILETIME lpCreationTime;
// creation time目录创建时间
FILETIME lpLastAccessTime;
// last access time目录最近访问时间
FILETIME lpLastWriteTime;
// last write time目录最近修改时间
// 获取目录日期和时间的Win32 API调用
BOOL retval = GetFileTime( hDir,&lpCreationTime,&lpLastAccessTime &lpLastWriteTime);
if ( retval )
{
FILETIME ftime;
FileTimeToLocalFileTime(&lpLastWriteTime, &ftime);
// 转换成本地时间
FileTimeToSystemTime(&ftime, &stime) ;
// 转换成系统时间格式}
CloseHandle(hDir);
return retval;
}
}
int DoTest(char DirName)
{
SYSTEMTIME stime;
printf(″testing for directory [%s]/n″, DirName) ;
// 显示修改前目录的时间
if ( GetDirTime(DirName, stime) )
printf(″before change is %04d-%02d-%02d %02d:%02d:%02d/n″, stime.wYear , stime.wMonth , stime.wDay , stime.wHour , stime.wMinute, stime.wSecond );
else
printf(″failed to get the datetime of directory.../n″);
stime.wYear = 1995;
stime.wMonth = 5;
stime.wDay = 12;
stime.wHour = 10 - 8;
// GMT time, GMT+8 for China PRC
stime.wMinute = 11;
stime.wSecond = 12;
// GetSystemTime(&stime);
// 如果要设置成当前的时间
// 修改目录的时间
if ( SetDirTime(DirName, stime) )
printf(″success to change datetime of directory./n″);
else
printf(″failed to change the datetime of directory.../n″);
// 显示修改后目录的时间
if ( GetDirTime(DirName, stime) )
printf(″after change is %04d-%02d-%02d %02d:%02d:%02d/n″,stime.wYear , stime.wMonth , stime.wDay ,stime.wHour , stime.wMinute, stime.wSecond );
else
printf(″failed to get the datetime of directory.../n″);
return 0;
}
void main(int argc, char argv[])
{
DoTest(″c://dir″);
}
相关文章推荐
- vc 修改MFC 消息循环的两种方法
- VC创建目录及取得系统时间的方法
- 用VC修改目录的日期和时间
- 用VC修改目录的日期和时间
- VC++/MFC程序图标更改方法以及修改程序标题 超级简单啦
- VC中关于时间获取及修改的方法
- vc中json文件的读取、修改和添加字段的方法
- 利用ADOX修改ACCESS表字段名(VC/MFC)
- VC 获取当前工作目录和执行目录的一些方法
- 读取文件的最后修改时间的方法
- VC获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
- Ext默认等待时间修改的小方法
- win2008 R2服务器下修改MySQL 5.5数据库data目录的方法
- linux下批量修改目录以及子目录里的所有文件的时间
- phpnow修改默认站点根目录的方法
- VC MFC 中进行时间比较
- VC++ 对话框工程MFC中父窗口和子窗口传值方法
- linux修改用户主目录的方法
- VC++ 使MFC程序在没有装VC的电脑上正常运行方法
- VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法