我确实有一个非常奇怪的问题!我想知道问题是在框架中,操作系统还是仅仅是我,误解了一些事情......
我有一个文件,可能是很久以前创建的,我使用该文件,然后我想通过更改它的名称来存档它.然后我想在重命名之前创建一个与旧文件同名的新文件.够容易!
真正困扰我的问题是,新创建的文件出错了"创建" - 时间戳!这是一个问题,因为它是我想用来确定何时存档和创建新文件的时间戳.
我创建了一个非常小的样本来显示问题.要使示例正常工作,Files文件夹中必须有一个文件1.txt.此外,还必须及时设置文件属性(使用其中一个工具,我使用Nomad.NET).
static void Main(string[] args)
{
// Create a directory, if doesnt exist.
string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\Files";
Directory.CreateDirectory(path);
// Create/attach to the 1.txt file
string filename = path + "\\1.txt";
StreamWriter sw = File.AppendText(filename);
sw.WriteLine("testing");
sw.Flush();
sw.Close();
// Rename it...
File.Move(filename, path + "\\2.txt");
// Create a new 1.txt
sw = File.AppendText(filename);
FileInfo fi = new FileInfo(filename);
// Observe, the old files creation date!!
Console.WriteLine(String.Format("Date: {0}", fi.CreationTime.Date));
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)