Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File is being used by another process

Tags:

c#

I have a program that roughly does this:

  1. open a file to read from it.
  2. close the file
  3. Start a filewatcher to watch for changes in the file.
  4. As soon as a change is detected, the filewatcher's EnableRaisingEvents flag is set to false and the process repeats from Step 1.

The problem is, after going from step 4 to step 1, it cannot read the file saying that it is being used by another Process.

Error I receive:

Unhandled Exception: System.IO.IOException: The process cannot access the file 'c:\test.xml' because it is being used by another process.

Whats going wrong? does the reader from Step 1 of my program still have the file open, or is some entirely different process accessing the file, or is it that filewatcher is still watching the file after moving to Step 1 from 4, despite setting the flag to false?

like image 742
xbonez Avatar asked Sep 17 '10 17:09

xbonez


People also ask

How do I fix a file that is being used by another process?

Press Ctrl + Alt + Delete, then click Task Manager. Ensure you're on the Processes tab. Locate and right-click SidekickTrayWPF, then select End Process. If a warning window opens to ask if you want to end the process, click End Process again to confirm.

What does it mean when a file is being used by another process?

This error usually means that either the user or someone else still has that file open. This can happen if the file referenced is open in another program or if a crash occurs while uploading.

How do I close a file that is open in another program?

Locate the File in Task Manager Look for the file in the Processes tab which shows you all the applications that are currently being used, whether with or without your knowledge. Select the file and tap on the “End Task” option near the bottom of the Manager window to stop the file from being used by a program.


1 Answers

If your code is similar to this:

[STAThread]
static void Main(string[] args)
{
    string file = "temp.txt";
    ReadFile(file);
    FileSystemWatcher fswatcher = new FileSystemWatcher(".\\");
    fswatcher.Changed += delegate(object sender, FileSystemEventArgs e)
                             {
                                 ReadFile(e.FullPath);
                             };
    while (true)
    {
        fswatcher.WaitForChanged(WatcherChangeTypes.Changed);
    }
}
private static void ReadFile(string file)
{
    Stream stream = File.OpenRead(file);
    StreamReader streamReader = new StreamReader(stream);
    string str = streamReader.ReadToEnd();
    MessageBox.Show(str);
    streamReader.Close();
    stream.Close();
}

If you are editing the file via notepad, then, when you click the save button, it keeps the file open, while as if when you just close the program and click save it doesn't. I do no know if this is a bug or an undocumented feature of notepad, but this just might be your problem. One way to fix this is to do the following:

In your anonymous delegate, or wherever you execute the call to ReadFile() call Thread.Sleep(1000), to have the program wait before reading the file and your code should work fine.

like image 93
Richard J. Ross III Avatar answered Sep 28 '22 19:09

Richard J. Ross III