在Windows 7中使用windows.h更改c ++中的文件创建日期 [英] Changing the File creation Date in c++ using windows.h in windows 7

查看:127
本文介绍了在Windows 7中使用windows.h更改c ++中的文件创建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码: -

I have the following code as:-

int main(int argc, char** argv) {
onelog a;
std::cout << "a new project";

ofstream file("C:\\users\\Lenovo\\Documents\\varuntest.txt", ios::app);//creates a fie     
as varuntest.txt
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;

}

只有当我检查文件的属性更改修改日期,我需要问如何更改文件的创建日期而不是修改日期。
感谢

now the question is it only changes the modified date when i check in the properties of the file i need to ask how to change the file's creation date instead of the modification date . thanks

请为此新手提供一些代码

please give some code for this novice

推荐答案

它设置最后修改的时间,因为这是你要求它做的。该函数接收3个文件时间参数,你只传递一个值到最后一个, lpLastWriteTime 。要设置创建时间,请调用以下函数:

It sets the last modified time because that's what you asked it to do. The function receives 3 filetime parameters and you only passed a value to the final one, lpLastWriteTime. To set the creation time call the function like this:

SetFileTime(filename, &thefiletime, (LPFILETIME) NULL,(LPFILETIME) NULL);

我建议您阅读 SetFileTime 。关键部分是其签名如下:

I suggest you take a read of the documentation for SetFileTime. The key part is its signature which is as follows:

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上查找。

Since you say that you are a novice with the Windows API I'll give you a tip. The documentation on MSDN is very comprehensive. Whenever you get stuck with a Win32 API call, look it up on MSDN.

对代码有一些注释:


  • 您应该始终检查任何API调用的返回值。如果你不正确地调用函数,或者因为某些其他原因而失败,你会发现无法检查错误而无法解决错误。

  • 您调用的变量 filename 实际上应该命名为 fileHandle

  • You should always check the return values for any API calls. If you call the functions incorrectly, or they fail for some other reason, you'll find it impossible to work out what went wrong without error checking.
  • The variable that you call filename should in fact be named fileHandle.

这篇关于在Windows 7中使用windows.h更改c ++中的文件创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆