0

I had the following code dispersed throughout my program. However I always seem to get the error below. Even though I am using a "using bracket" to dispose of resources I still don't know why this is happening.

Error:

The Process cannot access the file "the file path" because it is being used by another process.

Code:

string folderpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AGoogleHistory");
string filecreate;


private void restoreTbasToolStripMenuItem_Click(object sender, EventArgs e)
{
    using(StreamReader sr = new StreamReader(filecreate))
    {
        string s = sr.ReadToEnd();
        MessageBox.Show(s, "History", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

try
{
    browser.Navigate(new Uri(Address));
    using(StreamWriter sw = new StreamWriter(filecreate))
    {
        sw.WriteLine(Address);
    }
}
catch(System.UriFormatException)
{
    return;
}


private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Are You Sure", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    {
        File.Delete(filecreate);
    }
    else
    {

    }
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 1
    Your image was a link to the imgur homepage. I removed it. Please don't post images of errors anyway, just text, please. – Blorgbeard Apr 17 '15 at 03:52

2 Answers2

0

I think you should first check that file is exists or not by

File.Exists(filePath);

Second thing you are passing wrong parameter to StreamReader class which is an empty string as you haven't assigned anything to it. Check above mentioned things first and you can refer below link for your ease :

The process cannot access file...

Let me know if you have any other issue after trying this.

Community
  • 1
  • 1
Priyank Sheth
  • 2,352
  • 19
  • 32
0

StreamReader, by default, locks the file. This causes the error you are seeing. Luckily, StreamReader accepts a stream as one of it's overloads for it's constructor. This allows you to first create a FileStream, which has a handy enum allowing you to specify read/write sharing, then pass that FileStream to your StreamReader for use.

So in your case:

using(StreamReader sr = new StreamReader(filecreate))
...

becomes:

FileStream fs = new FileStream(filecreate, FileMode.Open, FileShare.ReadWrite);
using(StreamReader sr = new StreamReader(fs))
...

For more info, see the question below. It's essentially the same question, just asked differently. The accepted answer should explain a bit more.

How to open a StreamReader in ShareDenyWrite mode?

EDIT

Looking over your question again, I see that part of your problem is you don't close your streams. They should get closed when the using block exits, but it's best practice to close them yourself with sr.Close();

Also, you may need to add the delete flag to the fileshare option:

FileStream fs = new FileStream(filecreate, FileMode.Open, FileShare.ReadWrite|FileShare.Delete);
using(StreamReader sr = new StreamReader(fs))
...
Community
  • 1
  • 1
Ray Phillips
  • 95
  • 1
  • 7