【发布时间】:2013-10-28 17:37:49
【问题描述】:
当我尝试创建一个文件然后写入它时,我收到了错误The process cannot access the file 'C:\Users\Ryan\Desktop\New folder\POSData.txt' because it is being used by another process.。什么进程正在使用该文件?创建文件后,我检查了要调用的 file.close,但它不存在。我该如何度过这个难关?谢谢!
这是我的代码:
MessageBox.Show("Please select a folder to save your database to.");
this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
DialogResult result = this.folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
databasePath = folderBrowserDialog1.SelectedPath;
if (!File.Exists(databasePath + "\\POSData.txt"))
{
File.Create(databasePath + "\\POSData.txt");
}
using (StreamWriter w = new StreamWriter(databasePath + "\\POSData.txt", false))
{
w.WriteLine(stockCount);
}
}
编辑:仅在创建文件时发生。如果已经存在,则不会发生错误。
【问题讨论】:
-
仅供参考,您可以使用Process Explorer 来查看挂在文件上的进程。您可以搜索特定文件。
-
BTW 应该有 Googled... 第一个结果是完全相同的问题。关闭为重复。
标签: c# .net winforms file save