更改文件创建日期不起作用
我正在使用以下内容更改文本文件的创建日期:
using System.IO; ... DateTime newCreate = new DateTime(year, month, day, hour, minutes, seconds); File.SetCreationTime("changemydate.txt", newCreate);
然而,这没有任何作用。 没有错误消息,但它根本不会更改文件的日期。
我在Dropbox文件夹中以及随机文件夹中尝试了此操作但没有成功
DateTime newCreate
对象似乎是正确的。
如果有人能指出我的想法,那就太棒了……
实际上,每个文件有三个不同的时间 :
- 创作时间
- 上次访问时间
- 上次写入时间(在资源管理器和其他文件管理器中显示为“ 文件日期 ”)
要修改这些时间,您可以使用
File.SetCreationTime(path, time); File.SetLastWriteTime(path, time); File.SetLastAccessTime(path, time);
分别。
看来,如果你想改变文件管理器 (例如Explorer)中显示的文件日期,你应该尝试这样的事情:
String path = @"changemydate.txt"; DateTime time = new DateTime(year, month, day, hour, minutes, seconds); if (File.Exists(path)) File.SetLastWriteTime(path, time);
您可以使用此代码示例
string fileName = @"C:\MyPath\MyFile.txt"; if (File.Exists(fileName)) { DateTime fileTime = DateTime.Now; File.SetCreationTime(fileName, fileTime); }
我从来没有遇到过SetCreationTime的问题…但我认为你可以通过getter / setter CreationTime在FileSystemInfo上设置它。 也许这会更好地处理使用SetCreationTime的元信息缓存问题。
例如:
static void SetCreationTime(FileSystemInfo fsi, DateTime creationTime) { fsi.CreationTime = creationTime; }
再次感谢大家的帮助。 我现在一切都工作了,我和其他像我这样的初学者分享了所有的工作:
https://github.com/panditarevolution/filestamp
主要代码在/FileStamp/program.cs中
它是一个小命令行实用程序,允许更改文件的创建日期。 我用它作为一个小初学者的项目,教我一些关于c#和命令行界面的基础知识。 它使用了有用的CommandlineParser库: