如何用StreamReader打开被其他进程占用的文件

用StreamReader打开被其他进程占用的文件时,会收到The process cannot access the file 'xxxxx' because it is being used by another process.的错误信息。

采用下面的方式可以解决这个问题:

using(FileStream fs = new FileStream(@"xxx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (StreamReader sr = new StreamReader(fs))
    {
//Write your code here
    }
}

你可能感兴趣的:(C#,StreamReader)