[英]The process cannot access the file XXX because it is being used by another process
When I run my application, I get this error on the first "using" here: 当我运行我的应用程序时,在第一个“使用”位置出现此错误:
public void CreateFile(string filePath)
{
//Create File
string fileLoc = filePath;
FileStream fs = null;
if (!File.Exists(fileLoc))
{
using (fs = File.Create(fileLoc))
{
}
}
}
public void WriteFile(string filePath)
{
//Write to File
string fileLoc = filePath;
if (File.Exists(fileLoc))
{
using (StreamWriter sw = new StreamWriter(fileLoc))
{
sw.Write("Some sample text for the file");
}
}
}
A file from that directory was previously opened and read from, but the StreamReader was closed after it was read. 该目录中的文件先前已打开并从中读取,但在读取StreamReader之后将其关闭。 I'm not sure if that has any significance with this error.
我不确定此错误是否有意义。 Thanks.
谢谢。
您对出现错误的行之前的File.Exists
检查表明发生了奇怪的事情,例如尝试将目录路径用作filePath
或多线程竞争条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.