3

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.

Nathan
  • 1,287
  • 6
  • 15
  • 32
  • 1
    FYI you can use [Process Explorer](http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) to take a look at what process is hanging on to the file. You can do a search for a particular file. – tnw Oct 28 '13 at 17:42
  • BTW should have [Googled](https://www.google.com/search?q=c%23+file.create+file+in+use&oq=c%23+file.create+file+in+use&aqs=chrome..69i57j69i58j0l3.4389j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8)... first result is the exact same question. Closing as duplicate. – tnw Oct 28 '13 at 17:46
  • 1
    Duplicate of [File being used by another process after using File.Create()](http://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-create) – tnw Oct 28 '13 at 17:46

5 Answers5

3

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
}
tnw
  • 13,521
  • 15
  • 70
  • 111
0

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));
        }
Harrison
  • 3,843
  • 7
  • 22
  • 49
0

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.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
0

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.

spender
  • 117,338
  • 33
  • 229
  • 351
0

I used this and it worked

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

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