Nathan
Nathan

Reputation: 1347

"File in use" error when writing to text file

I'm getting the error The process cannot access the file 'C:\Users\Ryan\Desktop\New folder\POSData.txt' because it is being used by another process. when I try to create a file and then write to it. What process is using the file?? I checked for a file.close to call after I create the file, but it doesn't exist. How do I get past this? Thanks!

Heres my code:

MessageBox.Show("Please select a folder to save your database to.");
        this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
        DialogResult result = this.folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            databasePath = folderBrowserDialog1.SelectedPath;
            if (!File.Exists(databasePath + "\\POSData.txt"))
            {
                File.Create(databasePath + "\\POSData.txt");
            }

            using (StreamWriter w = new StreamWriter(databasePath + "\\POSData.txt", false))
            {
                w.WriteLine(stockCount);
            }
        }

Edit: Only happens when creating the file. If it already exists, no error occurs.

Upvotes: 3

Views: 2749

Answers (5)

Scott Berry
Scott Berry

Reputation: 1

I used this and it worked

`File.AppendAllText(fileName,"");`

This creates a new file, writes nothing to it, then closes it for you.

Upvotes: 0

Harrison
Harrison

Reputation: 3963

The File.Create returns a FileStream object that might need to be closed.

The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.

        using (FileStream fs = File.Create(databasePath + "\\POSData.txt"))
        {
             fs.Write(uniEncoding.GetBytes(stockCount), 0, uniEncoding.GetByteCount(stockCount));
        }

Upvotes: 0

tnw
tnw

Reputation: 13887

Actually, don't even bother using File.Create. The reason you're getting that error is because File.Create is opening up a stream on that text file.

string filePath = "databasePath + "\\POSData.txt"";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
    //write to the file
}

Upvotes: 4

spender
spender

Reputation: 120498

File.Create also opens the file for reading/writing. As such, you're leaving an open FileStream when you File.Create.

Assuming that overwriting is OK, then you probably want to do something like this:

        using (var fs = File.Create(databasePath + "\\POSData.txt"))
        using (StreamWriter w = new StreamWriter(fs))
        {
            w.WriteLine(stockCount);
        }

given that File.Create:

Creates or overwrites a file in the specified path.

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107576

You are keeping the file open when you call File.Create (i.e. you never close the file).

StreamWriter will create the file for you if it doesn't exist, so I wouldn't bother checking yourself. You could just remove the code that checks whether it exists and creates it if it doesn't.

if (result == DialogResult.OK)
{
    databasePath = folderBrowserDialog1.SelectedPath;

    using (StreamWriter w = new StreamWriter(databasePath + "\\POSData.txt", false))
    {
        w.WriteLine(stockCount);
    }
 }

Note that if the file doesn't exist, the second bool parameter in the StreamWriter constructor is ignored.

Upvotes: 0

Related Questions