Search code examples
c#file

Read and Write to File at the same time


for an application that uses a File as some sort of global storage for device reservations in a firm I need a way to read and write to a file (or lock a file, read from it, write to it, and unlock it). A little code snippet will shot what I mean:

FileStream in = new FileStream("storage.bin", FileMode.Open);
//read the file
in.Close();

//!!!!!
//here is the critical section since between reading and writing, there shouldnt
//be a way for another process to access and lock the file, but there is the chance
//because the in stream is closed
//!!!!!
FileStream out = new FileStream("storage.bin", FileMode.Create);
//write data to file
out.Close();

this should get something like this

LockFile("storage.bin");
//read from it...
//OVERwrite it....
UnlockFile("storage.bin");

the method should be absolute safe, since the program should run on 2000 devices at the same time


Solution

  • Simply holding a FileStream open with exclusive (not shared) access will prevent other processes from accessing the file. This is the default when opening a file for read/write access.

    You can 'overwrite' a file that you currently hold open by truncating it.

    So:

    using (var file = File.Open("storage.bin", FileMode.Open))
    {
        // read from the file
    
        file.SetLength(0); // truncate the file
    
        // write to the file
    }
    

    the method should be absolute safe, since the program should run on 2000 devices at the same time

    Depending on how often you're writing to the file, this could become a chokepoint. You probably want to test this to see how scalable it is.

    In addition, if one of the processes tries to operate on the file at the same time as another one, an IOException will be thrown. There isn't really a way to 'wait' on a file, so you probably want to coordinate file access in a more orderly fashion.