【问题标题】:C# FileStream StreamWriter another process exceptionC# FileStream StreamWriter 另一个进程异常
【发布时间】:2023-09-16 15:19:01
【问题描述】:

我正在从DataGridView 写入一个 CSV 文件。 当我在这个过程中打开文件时(当任务正在写入文件时)我收到一个错误,因为:

The process cannot access the file 'xyz\filename.csv' because it is being used by another process.

我尝试在每次写入后在 fileswOut 上手动调用 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 = "";
}

【问题讨论】:

  • 对不起,作为答案而不是评论发布。在右括号之前显示消息框。它显示了吗?
  • output_file 也是在哪里声明的。你想通过if (stop == true) output_file = ""; 实现什么stop 声明和设置在哪里?
  • stop 是一个公共布尔值,当我单击停止 btn 时变为真。但是此时总是假的所以它不能什么都不做。
  • @Peuczyński 是的,它显示消息框。

标签: c# datagridview filestream streamwriter flush


【解决方案1】:

为什么是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 = "";
}

【讨论】:

  • 我刚刚修改了我的代码,就像我上次的回答一样:问题是我每次都在打开和关闭(所以解锁)文件。
【解决方案2】:

确保使用代码中的文档的所有文件流都使用

关闭
      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();

确保所有文件流都已关闭,您将不再收到此错误

【讨论】:

  • 这怎么可能是一个解决方案? OP 已经使用了using,所以它会被自动处理掉。不是吗?
  • 我已经添加了 swOut.Close();和 file.Close();但没有任何改变
  • 我不是在谈论这段代码,我是说其他文件流代码使用他的项目中的文件
  • @aquanat 确保整个项目代码中的所有文件流都已关闭,我多次遇到同样的问题,这始终是原因
  • 如果您仅在此代码中使用文件流,那么我想我不知道解决方案
【解决方案3】:

好的,解决办法就到这里。

在我宣布之前

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 始终处于打开状态,并且不会像我的原始代码那样每次都关闭。

谢谢!

【讨论】: