在windows 7中使用windows.h更改C++中的文件创建日期
Changing the File Creation Date in C++ using windows.h in Windows 7
我有以下代码:
int main(int argc, char** argv) {
onelog a;
std::cout << "a new project";
//creates a file as varuntest.txt
ofstream file("C:\users\Lenovo\Documents\varuntest.txt", ios::app);
SYSTEMTIME thesystemtime;
GetSystemTime(&thesystemtime);
thesystemtime.wDay = 07;//changes the day
thesystemtime.wMonth = 04;//changes the month
thesystemtime.wYear = 2012;//changes the year
//creation of a filetimestruct and convert our new systemtime
FILETIME thefiletime;
SystemTimeToFileTime(&thesystemtime,&thefiletime);
//getthe handle to the file
HANDLE filename = CreateFile("C:\users\Lenovo\Documents\varuntest.txt",
FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
//set the filetime on the file
SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&thefiletime);
//close our handle.
CloseHandle(filename);
return 0;
}
现在的问题是;它只会在我检查文件属性时更改修改日期。我需要问;
如何更改文件的创建日期而不是修改日期
感谢
请给这个新手一些代码。
它设置上次修改的时间,因为这是您要求它做的。该函数接收3个文件时间参数,而您只向最后一个参数lpLastWriteTime
传递了一个值。要设置创建时间,请调用以下函数:
SetFileTime(filename, &thefiletime, (LPFILETIME) NULL,(LPFILETIME) NULL);
我建议您阅读SetFileTime
的文档。关键部分是其签名,如下所示:
BOOL WINAPI SetFileTime(
__in HANDLE hFile,
__in_opt const FILETIME *lpCreationTime,
__in_opt const FILETIME *lpLastAccessTime,
__in_opt const FILETIME *lpLastWriteTime
);
既然你说你是Windows API的新手,我就给你一个提示。MSDN上的文档非常全面。每当遇到Win32 API调用时,请在MSDN上查找它。
以及对你的代码的一些评论:
- 您应该始终检查任何API调用的返回值。如果您错误地调用了这些函数,或者它们由于其他原因而失败,您将发现如果不进行错误检查,就不可能找出问题所在
- 您调用的
filename
变量实际上应该命名为fileHandle
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux for Windows上编译C++代码时出错
- 在Windows上用C++裁剪HBITMAP
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 在Windows中以.exe的形式运行c++
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 无法在windows控制台中为C++程序提供必要的输入
- 在Windows中查找扬声器输出的当前音量级别
- Windows.h与GLFW.h的接口
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在Linux和C++中的Windows上,散列字符串值会产生不同的输出
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 无法使用Qt Creator在Windows中构建yaml-cpp