System.IO.IOException:The process cannot access the file 'XX.xml' because it is being used by another process. 解决方案

有时候用StreamReader去读取xml文件时,会出现System.IO.IOException:The process cannot access the file 'XX.xml' because it is being used by another process. 错误,这是因为

string str = ""; 
StreamReader reader = new StreamReader( "E:\test.xml", Encoding.Default );
str = reader.ReadToEnd();

reader在读取完毕后,没有没有关闭reader和释放资源,xml文件在被占用着,所以报错。

在后面增加代码

 reader.Close();
 reader.Dispose();
原文地址:https://www.cnblogs.com/jery0125/p/2468359.html