读书人

System.IO 文件正由另一进程使用因此

发布时间: 2012-05-16 11:12:12 作者: rapoo

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(); } 

读书人网 >C#

热点推荐