↓ Archives ↓

Reading a text file without locking the file.

Often time, we want to read a text file that other process is writing onto. In C#, FileStream attempts to lock the file by default. This is not desirable if there is a process put a lock on the file already; FileStream won’t be able to read the file. Here is a simple code to read a text file while other process is writing onto it.


FileStream fs = null;
StreamReader sr = null;
try {
    fs = new FileStream(string_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    sr = new StreamReader(fs);
} catch (FileNotFoundException e){
    // exception handling
}

1 Comment

  • Apr 16th 200902:04
    by Atmotobaw

    hmm. interesting )

  • Leave a Reply