IOException-另一个进程正在使用FileShare.ReadWrite的文件



我一直在为我的问题寻找解决方案,尝试了很多可能性,但当另一个进程使用文件内容时,我仍然无法读取。我希望有人能帮忙:(

using (FileStream fs = File.OpenWrite(file)) //Here I try to simulate that the file is opened for read.
{
Assert.NotNull(xmlUtility.Load(file));
}

这是我想如何打开它进行读取访问的代码:

using (StreamReader fs = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
doc = XDocument.Load(fs);
}

看起来根本原因是我对打开的文件的模拟。那个电话";File.OpenWrite(("使用FileAccess打开文件。None将禁用对文件的其他访问。

感谢Sidewinder94:(

如果我使用其他方法来模拟打开的文件,那么给定的示例也可以工作。

你能用记事本打开它吗。。。单词

using (var fs = new FileStream(path, FileMode.Open, 
FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs, Encoding.Default)) {
// read the stream
}

最新更新