使用只读属性设置文件的时间戳现在在 Windows 上成功,不再引发异常。
旧行为
在 .NET 6 维护版本之前,在 Windows 只读文件上设置时间戳会导致出现 UnauthorizedAccessException。
新行为
从 .NET 6.0.2 开始,在 Windows 上的只读文件上设置时间戳成功。
已引入的版本
.NET 6.0.2 (服务版本)
破坏性变更的类型
此更改可能会影响 二进制兼容性。
更改原因
客户提供反馈,指出他们预期在只读文件上设置时间戳以成功。 此更改还使 Windows 行为与 Linux 保持一致。 最后,行为是无意的,是由 bug 引起的。
建议的措施
现有代码不太可能期望在只读文件上设置时间戳会失败。 但是,如果你的代码希望它失败,那么在尝试设置时间戳之前,请使用 File.GetAttributes(String) 添加对只读属性的检查。
受影响的 API
- 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)