符号链接上的时间字段
更改符号链接(“symlink”)上的以下时间相关字段时,更新现在会影响符号链接本身,而不是目标:
旧行为
以前,更新符号链接上与时间相关的任何字段都会影响符号链接目标的字段。
我们以下面的程序为例,该程序打印文件及其符号链接上的各种时间字段值、将符号链接的时间字段值更新为 1 天后,然后在文件和符号链接上重新打印时间字段值。
string filename = "file";
string linkname = "link";
// Create a file and symlink.
File.Create(filename).Dispose();
File.CreateSymbolicLink(linkname, filename);
Console.WriteLine("Before update:");
PrintMetadata(filename);
PrintMetadata(linkname);
UpdateMetadata(linkname);
Console.WriteLine("\nAfter update:");
PrintMetadata(filename);
PrintMetadata(linkname);
static void UpdateMetadata(string filename)
{
DateTime tomorrow = DateTime.Now.AddDays(1);
File.SetCreationTime(filename, tomorrow);
File.SetLastAccessTime(filename, tomorrow);
File.SetLastWriteTime(filename, tomorrow);
File.SetAttributes(filename, File.GetAttributes(filename) | FileAttributes.Offline);
}
static void PrintMetadata(string filename)
{
Console.WriteLine($"---{filename}---");
Console.WriteLine("Creation:\t" + File.GetCreationTime(filename));
Console.WriteLine("Last access:\t" + File.GetLastAccessTime(filename));
Console.WriteLine("Last write:\t" + File.GetLastWriteTime(filename));
Console.WriteLine("Attributes:\t" + File.GetAttributes(filename));
}
在以前,更新符号链接上的值后,仅目标文件的时间字段会被更新。 上述程序的输出如下所示:
Before update:
---file---
Creation: 9/29/2022 10:35:40 AM
Last access: 9/29/2022 10:35:40 AM
Last write: 9/29/2022 10:35:40 AM
Attributes: Archive
---link---
Creation: 9/29/2022 10:35:40 AM
Last access: 9/29/2022 10:35:40 AM
Last write: 9/29/2022 10:35:40 AM
Attributes: Archive, ReparsePoint
After update:
---file---
Creation: 9/30/2022 10:35:40 AM
Last access: 9/30/2022 10:35:40 AM
Last write: 9/30/2022 10:35:40 AM
Attributes: Archive
---link---
Creation: 9/29/2022 10:35:40 AM
Last access: 9/29/2022 10:35:40 AM
Last write: 9/29/2022 10:35:40 AM
Attributes: Archive, ReparsePoint, Offline
新行为
从 .NET 7 开始,更新符号链接上与时间相关的任何字段会影响符号链接本身的字段,而不是目标文件的字段。
旧版行为部分显示的程序输出如下:
Before update:
---file---
Creation: 9/29/2022 10:33:39 AM
Last access: 9/29/2022 10:33:39 AM
Last write: 9/29/2022 10:33:39 AM
Attributes: Archive
---link---
Creation: 9/29/2022 10:33:39 AM
Last access: 9/29/2022 10:33:39 AM
Last write: 9/29/2022 10:33:39 AM
Attributes: Archive, ReparsePoint
After update:
---file---
Creation: 9/29/2022 10:33:39 AM
Last access: 9/29/2022 10:33:39 AM
Last write: 9/29/2022 10:33:39 AM
Attributes: Archive
---link---
Creation: 9/30/2022 10:33:39 AM
Last access: 9/30/2022 10:33:39 AM
Last write: 9/30/2022 10:33:39 AM
Attributes: Archive, ReparsePoint, Offline
引入的版本
.NET 7 预览版 1
中断性变更的类型
此项更改可能会影响二进制兼容性。
更改原因
在部分情况下,旧版行为是让人意外且不满的:
- 它与获取相同字段的属性和方法的行为不一致。
- 也不可能实际使用 .NET API 更新符号链接本身的字段。
建议的操作
如果你之前依靠该行为来设置符号链接的目标上的值,那么以后设置符号链接中的某一 *Time
字段将不再影响目标。 可以使用新的符号链接 API 获取符号链接的目标,然后改为更新对应的文件系统对象。
FileSystemInfo? targetInfo = linkInfo.ResolveLinkTarget(returnFinalTarget: true);
if (targetInfo != null)
{
// Update the properties accordingly.
targetInfo.LastWriteTime = DateTime.Now;
}
受影响的 API
- System.IO.Directory.SetCreationTime(String, DateTime)
- System.IO.Directory.SetCreationTimeUtc(String, DateTime)
- System.IO.Directory.SetLastAccessTime(String, DateTime)
- System.IO.Directory.SetLastAccessTimeUtc(String, DateTime)
- System.IO.Directory.SetLastWriteTime(String, DateTime)
- System.IO.Directory.SetLastWriteTimeUtc(String, DateTime)
- System.IO.File.SetCreationTime(String, DateTime)
- System.IO.File.SetCreationTimeUtc(String, DateTime)
- System.IO.File.SetLastAccessTime(String, DateTime)
- System.IO.File.SetLastAccessTimeUtc(String, DateTime)
- System.IO.File.SetLastWriteTime(String, DateTime)
- System.IO.File.SetLastWriteTimeUtc(String, DateTime)
- System.IO.FileSystemInfo.CreationTime
- System.IO.FileSystemInfo.CreationTimeUtc
- System.IO.FileSystemInfo.LastAccessTime
- System.IO.FileSystemInfo.LastAccessTimeUtc
- System.IO.FileSystemInfo.LastWriteTime
- System.IO.FileSystemInfo.LastWriteTimeUtc