我正在从DataGridView
. 当我在此过程中打开文件时(当任务正在写入文件时)我收到一个错误,因为:
The process cannot access the file 'xyz\filename.csv' because it is being used by another process.
我已经尝试在每次写入后手动调用该Flush()
方法,file
但仍然出现错误。swOut
我需要使用FileMode.Append
,因为创建文件后我添加了 n 行。我已经尝试了每个不同的FileShare
枚举选项,但没有任何效果。
我哪里错了?
我的代码:
using (FileStream file = new FileStream(output_file, FileMode.Append, FileAccess.Write, FileShare.Read))
{
StreamWriter swOut = new StreamWriter(file);
swOut.AutoFlush = true;
if (table.Rows.Count == 2)
{
for (int i = 0; i <= table.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(",");
}
swOut.Write(table.Columns[i].HeaderText);
}
swOut.WriteLine();
}
swOut.Write(category.Replace(",", " ") + "," + name.Replace(",", " ") + "," + address.Replace(",", " ") + "," + locality.Replace(",", " ") + "," + cap.Replace(",", " ") + "," + tel.Replace(",", " ") + "," + fax.Replace(",", " ") + "," + www.Replace(",", " ") + "," + email_results.Replace(",", " ") + "," + business_url.Replace(",", " ") + "," + map_query.Replace(",", " "));
swOut.WriteLine();
file.Close();
if (stop == true) output_file = "";
}