3

When using a file stream, and setting FileShare to None, and say two users accessing the same function at the same time want to read/write to that file. Will FileShare.None make the second users request waiting or will the second user's request throw an exception?

//two users get to this this code at the same time

using (FileStream filestream = new FileStream(chosenFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
using (StreamReader sr = new StreamReader(filestream))
using (StreamWriter sw = new StreamWriter(filestream))
{
    //reading and writing to file
}

Msdn says: None Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.

But will requests keep trying until the filestream is closed?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Satbir Kira
  • 792
  • 6
  • 21

2 Answers2

3

When a process opean a file for Read/Write with FileShare.None any subsequent access by any process on this same file will result in Acess Denied Exception. To answer your question, Second user will get exception.

MSDN: FileShare.None - Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.


There are many ways you can handle these kind of concurrent file access issues, Following code demonstrates a simple approach to tackle this situation.

//Retry 5 times when file access fails
int retryCounter = 5;

while (!isFileAccessSuccess && retryCounter > 0)
{
    try
    {
       //Put file access logic here

        //If the file has been accessed successfully set the flag to true
        isFileAccessSuccess = true;
    }
    catch (Exception exception)
    {
        //Log exception
    }
    finally
    {
        //Decrease the retry count
        --retryCounter;
    }

    if (!isFileAccessSuccess)
    {
        //Wait sometime until initiating next try
        Thread.Sleep(10000);
    }
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
3

No, IOException will be thrown an with HResult = -2147024864 and Message = The process cannot access the file 'path' because it is being used by another process.

if you want to synchronize access to a file you can use a named wait handle.

public class FileAcessSynchronizer
{
    private readonly string _path;
    private readonly EventWaitHandle _waitHandle;

    public FileAcessSynch(string path)
    {
        _path = path;
        _waitHandle =  new EventWaitHandle(true, EventResetMode.AutoReset, "NameOfTheWaitHandle");
    }

    public void DoSomething()
    {
        try
        {
            _waitHandle.WaitOne();
            using (FileStream filestream = new FileStream(chosenFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
            using (StreamReader sr = new StreamReader(filestream))
            using (StreamWriter sw = new StreamWriter(filestream))
            {
                //reading and writing to file
            }
        }
        finally
        {
            _waitHandle.Set();
        }
    }
}

since named wait handle creates a critical section no two threads or processes of your application (that use same name as wait handle name) can execute the codes in it concurrently. So one thread or process enters the section, opens the file in the way that no one can access it (other applications), execute commands and at the end leaves the critical section to allow other threads or processes of your application enters the critical section.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • And since the OP refers to threads, it's worth pointing out that the error message is not necessarily correct. You get that same error message regardless of which process has the file locked, you get it even if it's the same process as the one attempting to open it. –  Jun 12 '15 at 09:53
  • Can you write this code instance in the case of a file stream like the question I asked? – Satbir Kira Jun 12 '15 at 10:07
  • @dotctor The file will be accessed by two different .net pages in two different functions. I'm sorry I didn't mention that earlier. It looks like kurubaran solution is my best bet. – Satbir Kira Jun 12 '15 at 10:38