如果用下面这个方法写流操作,会出现“正由另一进程使用,因此该进程无法访问此文件”的错误。
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter writer = new StreamWriter(fs);
writer.Write(log);
writer.Write("\r\n");
writer.Close();
fs.Close();
//解决办法可以写成这样,在using中操作。
//参考http://www.cnblogs.com/DanielX/archive/2007/09/21/901067.html
using (FileStream fs = new FileStream(path, FileMode.Append))
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.Write(log);
writer.Write("\r\n");
}
}