更改文件创建日期无效 [英] Changing the file creation date does not work

查看:99
本文介绍了更改文件创建日期无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方式更改文本文件的创建日期:

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:


  1. 创建时间

  2. 最后访问时间

  3. 最后写入时间(在资源管理器和其他文件管理器中显示为 文件日期

  1. Creation time
  2. Last access time
  3. 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屋!

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