aquanat
aquanat

Reputation: 2137

C# FileStream StreamWriter another process exception

I'm writing a CSV file from a DataGridView. When I open the file during the process (while the task is writing to the file) I get an error because:

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

I've tried manually calling the Flush() method on both file and swOut after each write but I still get the error.

I need to use FileMode.Append because after file is created I add n-rows. I've tried each of the different options for the FileShare enumeration, but nothing is working.

Where am I wrong?

My code:

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 = "";
}

Upvotes: 0

Views: 2220

Answers (3)

aquanat
aquanat

Reputation: 2137

Ok, here the solution.

Before i declared

public FileStream file;

in main class.

Then when it starts to add rows to datagridview, i initialize the filestream:

file = new FileStream(output_file, FileMode.Append, FileAccess.Write, FileShare.Read);

Then every time i add a row, i add a row also in csv with this code:

 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);

So FileStream is always open and it doesn't close every time as in my original code.

Thanks!

Upvotes: 0

matzone
matzone

Reputation: 5719

Why file.Close(); ?

it should be swOut.Close();

EDITED :

Or like this

'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 = "";
}

Upvotes: 1

user2399866
user2399866

Reputation:

Make sure all filestream using the document in your code are closed using

      Close();

Example :

    //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();

Make sure all the filestreams are closed and you will no longer get this error

Upvotes: 0

Related Questions