我正在尝试检测运行时是否存在文件,如果没有,则创建它.但是当我尝试写入时,我收到此错误:
该进程无法访问文件'myfile.ext',因为它正由另一个进程使用.
string filePath = string.Format(@"{0}\M{1}.dat", ConfigurationManager.AppSettings["DirectoryPath"], costCentre);
if (!File.Exists(filePath))
{
File.Create(filePath);
}
using (StreamWriter sw = File.AppendText(filePath))
{
//write my text
}
Run Code Online (Sandbox Code Playgroud)
关于如何修复它的任何想法?