每当我尝试保存现有文件时,都会收到此错误:
该进程无法访问该文件,因为它正被另一个进程使用
当文件不存在时它可以工作,但是当我尝试再次写入时,会出现错误。当时我没有访问或使用该文件。
这是代码:
string directory = filepath + "Updates\\" + dir;
if(!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
remarks = remarks.Trim();
remarks = remarks.Replace("\r\n","<br>");
remarks = remarks.Replace(",","|");
string file = directory + "\\" + getIndex(barcode) + ".asdt";
StreamWriter writer = new StreamWriter(file,true);
writer.WriteLine(username + "," + barcode + "," + DateTime.Now.ToString("yyyy-MM-dd HH.mm")+ "," + remarks);
writer.Close();
当我检查它时,错误发生在该行:
StreamWriter writer = new StreamWriter(file,true);
这可能是什么原因?