问题描述
我需要更新文件内容以及文件创建时间和其他元数据。这是我需要做的:
这是我的代码:
FileInfo file1 = new FileInfo("C:\\path\file1.ext");
await using (FileStream stream1 = file1.Open(FileMode.Open,FileAccess.Read,FileShare.Read))
{
FileInfo file2 = new FileInfo("C:\\path\file2.ext");
// If another thread is trying to update the same file,this must fail in other threads.
await using (FileStream stream2 = file2.Open(FileMode.Truncate,FileAccess.Write,FileShare.None))
{
// Update file content.
await stream1.copyToAsync(stream2);
// This call fails with IOException "The process can not access the file because it is being used by another process"
file2.CreationTimeUtc = new DateTime(2020,8,20);
}
}
在更新内容和更新元数据之间,没有其他线程应该能够读取file2内容或更新file2。 .NET完全可以吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)