[英]C# FileStream StreamWriter another process exception
我正在從DataGridView
編寫CSV文件。 在此過程中(任務正在寫入文件時)打開文件時,出現錯誤,因為:
The process cannot access the file 'xyz\\filename.csv' because it is being used by another process.
我嘗試在每次寫入后在file
和swOut
上手動調用Flush()
方法,但仍然收到錯誤。
我需要使用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 = "";
}
為什么file.Close();
?
它應該是swOut.Close();
編輯:
或者像這樣
'using (FileStream file = new FileStream(output_file, FileMode.Append, FileAccess.Write, FileShare.Read))
using (StreamWriter swOut = new File.AppendText(output_file))
{
'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();
swOut.Close();
if (stop == true) output_file = "";
}
確保使用以下代碼關閉使用代碼中文檔的所有文件流
Close();
范例:
//Create the File
FileStream FS = new FileStream("C:/Path to document",FileMode.Append,FileAccess.Write);
//Call the Close Method to Close the file
FS.Close();
確保所有文件流均已關閉,並且不會再出現此錯誤
好的,這是解決方案。
在我宣布之前
public FileStream file;
在主要班級。
然后,當它開始向datagridview添加行時,我初始化文件流:
file = new FileStream(output_file, FileMode.Append, FileAccess.Write, FileShare.Read);
然后每次我添加一行時,我也使用以下代碼在csv中添加一行:
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();
swOut.Close();
file.Close();
file = new FileStream(output_file, FileMode.Append, FileAccess.Write, FileShare.Read);
//file.Lock(0, file.Length);
因此,FileStream始終處於打開狀態,並且不會像我的原始代碼一樣每次都關閉。
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.