首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

System.IO 文件正由另一进程使用,因此该进程无法访问该文件解决思路

2012-05-16 
System.IO 文件正由另一进程使用,因此该进程无法访问该文件stringPathd:\\my.txt File.Create(Path)S

System.IO 文件正由另一进程使用,因此该进程无法访问该文件
string Path = "d:\\my.txt "; 
File.Create(Path); 
StreamWriter sw = new StreamWriter(strPath, false); 
string s = hi.Value; 
sw.Write(s); 
sw.Close(); 

错误 
文件“d:\\my.txt ”正由另一进程使用,因此该进程无法访问该文件。

[解决办法]
string Path = "d:\\my.txt "; 
FileStream stream=File.Create(Path); 
StreamWriter sw = new StreamWriter(stream, false); 
string s = hi.Value; 
sw.Write(s); 
sw.Close(); 

create是产生一个stream
你没有关闭
而你又用
streamwriter访问
当然出错
[解决办法]

C# code
using(StreamWriter  sw  =  new  StreamWriter(strPath,  false)){string  s = hi.Value; sw.Write(s); sw.Close(); } 

热点排行