C#FileStream StreamWriter另一个进程异常

C#FileStream StreamWriter另一个进程异常,c#,datagridview,filestream,streamwriter,flush,C#,Datagridview,Filestream,Streamwriter,Flush,我正在从DataGridView编写一个CSV文件。 当我在过程中打开文件时(当任务写入文件时),我会收到一个错误,因为: 该进程无法访问文件“xyz\filename.csv”,因为另一进程正在使用该文件。 每次写入后,我都尝试在文件和swOut上手动调用Flush()方法,但仍然出现错误 我需要使用FileMode.Append,因为在创建文件后,我添加了n行。我已经尝试了FileShare枚举的每个不同选项,但都不起作用 我错在哪里 我的代码: using (FileStream file

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

该进程无法访问文件“xyz\filename.csv”,因为另一进程正在使用该文件。

每次写入后,我都尝试在
文件
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 = "";
}
使用(FileStream file=newfilestream(输出文件,FileMode.Append,FileAccess.Write,FileShare.Read))
{
StreamWriter swOut=新的StreamWriter(文件);
swOut.AutoFlush=true;
if(table.Rows.Count==2)
{
对于(int i=0;i 0)
{
写出(“,”);
}
Write(table.Columns[i].HeaderText);
}
swOut.WriteLine();
}
swOut.Write(category.Replace(“,”,“)+”,“+name.Replace(“,”,“)+”,“+address.Replace(“,”,“,”)+”,“+locality.Replace(“,”,“,”)”,“+cap.Replace(“,”,“,”)+”,“+tel.Replace(“,”,“,”,“,”,“,”,“,”)+”,“,“+fax.Replace(,”,“,”,“,”,“,“,”,”,“,“,”,“,”,“,“,”,”,“,“,”,”,“,“,”,”,“,”,“,“+map_query.Replace(“,”,”);
swOut.WriteLine();
file.Close();
如果(stop==true)输出_file=“”;
}

确保所有使用代码中文档的filestream都已使用关闭

      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();
确保所有文件流都已关闭,您将不会再收到此错误

为什么
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 = "";
}
使用(FileStream file=newfilestream(输出文件,FileMode.Append,FileAccess.Write,FileShare.Read)) 使用(StreamWriter swOut=new File.AppendText(输出文件)) { 'StreamWriter swOut=新的StreamWriter(文件); swOut.AutoFlush=true; if(table.Rows.Count==2) { 对于(int i=0;i 0) { 写出(“,”); } Write(table.Columns[i].HeaderText); } swOut.WriteLine(); } swOut.Write(category.Replace(“,”,“)+”,“+name.Replace(“,”,“)+”,“+address.Replace(“,”,“,”)+”,“+locality.Replace(“,”,“,”)”,“+cap.Replace(“,”,“,”)+”,“+tel.Replace(“,”,“,”,“,”,“,”,“,”)+”,“,“+fax.Replace(,”,“,”,“,”,“,“,”,”,“,“,”,“,”,“,“,”,”,“,“,”,”,“,“,”,”,“,”,“,“+map_query.Replace(“,”,”); swOut.WriteLine(); swOut.Close(); 如果(stop==true)输出_file=“”; }
好的,这里是解决方案

在我宣布之前

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);
StreamWriter swOut=新的StreamWriter(文件);
swOut.AutoFlush=true;
if(table.Rows.Count==2)
{
对于(int i=0;i 0)
{
写出(“,”);
}
Write(table.Columns[i].HeaderText);
}
swOut.WriteLine();
}
swOut.Write(category.Replace(“,”,“)+”,“+name.Replace(“,”,“)+”,“+address.Replace(“,”,“,”)+”,“+locality.Replace(“,”,“,”)”,“+cap.Replace(“,”,“,”)+”,“+tel.Replace(“,”,“,”,“,”,“,”,“,”)+”,“,“+fax.Replace(,”,“,”,“,”,“,“,”,”,“,“,”,“,”,“,“,”,”,“,“,”,”,“,“,”,”,“,”,“,“+map_query.Replace(“,”,”);
swOut.WriteLine();
swOut.Close();
file.Close();
file=newfilestream(输出文件,FileMode.Append,FileAccess.Write,FileShare.Read);
//file.Lock(0,file.Length);
所以FileStream总是打开的,而不是像我的原始代码那样每次都关闭


谢谢

抱歉,作为答案而不是评论发布。在右括号前显示messagebox。它显示了吗?还显示了
输出文件
声明的位置。如果(stop==true)output_file=“”,您试图通过
实现什么在哪里声明和设置了
stop
?stop是一个公共bool,当我点击stop btn时变为true。但此时总是错误的,所以它不能做任何事情。@Peuczyński是的,它显示messagebox。这怎么可能是一个解决方案?OP已使用
使用
,因此它将被自动禁用。不是吗?我已经添加了swOut.Close();和file.Close();但是没有任何改变我不是在说这个代码,我是说其他文件流代码在他的文件中使用这个文件Project@aquanat确保整个项目代码中的所有文件流都已关闭,我曾多次遇到过同样的问题,这也是问题的原因。如果您只在这段代码中使用filestream,那么我想我不知道解决方案。我只是像上次回答中那样修改了代码:问题是我每次都在打开和关闭(解锁)文件。