更改文件创建日期无效 [英] Changing the file creation date does not work
问题描述
我正在使用以下方式更改文本文件的创建日期:
I'm using the following to change the creation date of a text file:
using System.IO;
...
DateTime newCreate = new DateTime(year, month, day, hour, minutes, seconds);
File.SetCreationTime("changemydate.txt", newCreate);
但是,此操作无济于事。没有错误消息,但它根本不会更改文件的日期。
However this doesn't do anything. There is no error message, yet it doesn't change the date of the file at all.
我在Dropbox文件夹以及没有文件夹的随机文件夹中都尝试过成功
I tried this in a dropbox folder as well as in a random folder without success
DateTime newCreate
对象似乎是正确的。
如果有人可以给我指出一个主意,那就太好了……
It would be great if somebody could point me to an idea...
推荐答案
实际上,每个文件都有三个不同的时间:
Actually, each file has three different times:
- 创建时间
- 最后访问时间
- 最后写入时间(在资源管理器和其他文件管理器中显示为 文件日期)
- Creation time
- Last access time
- Last write time (that's shown in Explorer and other file managers as "File Date")
要修改这些时间,您可以使用
To modify these times you can use
File.SetCreationTime(path, time);
File.SetLastWriteTime(path, time);
File.SetLastAccessTime(path, time);
分别。
似乎,如果您想更改文件日期(如文件管理器中所示)(例如资源管理器),则应尝试以下操作:
It seems, that if you want to change file date as it shown in file manager (e.g. Explorer) you should try something like that:
String path = @"changemydate.txt";
DateTime time = new DateTime(year, month, day, hour, minutes, seconds);
if (File.Exists(path))
File.SetLastWriteTime(path, time);
这篇关于更改文件创建日期无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!