Stream Reader process cannot access file because its in use by another process

21,800

Solution 1

We can use a different signature for opening the filestream with read/write access to other processes:

Stream stream = new FileStream(fileToRead, FileMode.Open, FileAccess.Read,
                        FileShare.ReadWrite);

or

Stream stream = File.Open(fileToRead, FileMode.Open, FileAccess.Read,
                        FileShare.ReadWrite);

The FileShare option determines how other processes can access the same file when this process opens the same file.

Your first code block will default to FileShare.None:

Stream stream = new FileStream(fileToRead, FileMode.Open, FileAccess.Read);

This would fail whilst the file is open as it's trying to obtain exclusive access to the file.

However, you would need this to occur in the log writer to allow your log reader to have read access.

Lastly, try running your log reader as an administrator, as there may be operating system permissions at play that are not obvious when you "open with notepad".

Solution 2

I had a similar problem that was resolved by setting the file attributes.

File.SetAttributes(outputDirectory + fileName + ".txt", FileAttributes.Normal);

This was done after the file was created, though. The problem has to do with how the OS manages files that are being accessed by multiple processes. In my case, I deleted the file (you aren't always allowed to do this because you'll get the error you're seeing). The only real way to solve this is to track down the processes that are accessing the file and stop them. The easiest way I fix this is restarting my PC, but if you are working on a server, that isn't feasible to do.

I haven't done an extensive amount of testing, but if you wanted to get this to be used by multiple processes, maybe it might be a good idea to give the file read properties and/or attributes:

File.SetAttributes(outputDirectory + fileName + ".txt", FileAttributes.ReadOnly);

Good luck - hope this helps someone else out there (since this is already a year old!).

Solution 3

Here is a code snippet

The problem is when another application opens it first and the file sharing is Read only (FileShare.Read) certainly you can't write onto the file but you can read, Or file sharing is Write only (FileShare.Write) you can't Read onto the file but still you can be write onto it.

 using (FileStream file = new FileStream("You File Name here", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            using (StreamReader sr = new (StreamReader (file))
            {
        //Do your codes here     
            }
        }
Share:
21,800
SlopTonio
Author by

SlopTonio

Updated on April 12, 2020

Comments

  • SlopTonio
    SlopTonio about 4 years

    My application parses log files but when trying to parse the current day's file I get an error stating that the file is being used by another process. This log file is currently being written to and can be accessed through notepad but not through my application.

    Current Code:

    Stream stream = new FileStream(fileToRead, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader(stream);
    

    Also tried this but had no luck:

    Stream stream = new FileStream(fileToRead, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    

    What changes need to be to my code in order to READ a file that is being used by another process. Copying the log file is not a solution due to the size of the log and performance of my application

  • SlopTonio
    SlopTonio about 10 years
    I've tried FileShare.Read, FileShare.Write and FileShare.ReadWrite but had no luck. Any other suggestions?
  • Kevin Hogg
    Kevin Hogg about 10 years
    As mentioned you need the log writer to open the file stream with FileShare.Read; check the log writer code and determine if it uses FileShare.Read when creating the new log file.
  • SlopTonio
    SlopTonio about 10 years
    My program does not write/create this log file that I cannot access. It is being written to by one of my company websites. I do not have access to that code.
  • SlopTonio
    SlopTonio about 10 years
    I tried the above code but instead of StreamWriter I'm using StreamReader because I am trying to read the file not write to it. I am still unable to access the file
  • Jade
    Jade about 10 years
    @SlopTonio, see my updates
  • SlopTonio
    SlopTonio about 10 years
    as I previously commented, I tried that but still unable to access the file. It's weird because notepad can open the log file but my application can't
  • Hi-Angel
    Hi-Angel over 7 years
    Beware of a bug: I was getting the same error; after a long research the reason turned out that subsequent opening of a file with access with less FileShare leads to an Exception. (in my case the file first opened as a FileStream with FileShare.ReadWrite, next opened again as a using (var stream = File.OpenRead(file)), it didn't have the option, but most probably it sets it to None)