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();
posted @ 2012-04-24 17:01  ~夜~  阅读(2030)  评论(0编辑  收藏  举报