Dear All,
I am developing a software and i need to write a data on .txt file at the same time i need to read data for another purpose on timer's every second but i am getting an error "The process cannot access the file 'e:\pctext.txt' because it is being used by anothe"

if possible then solve the problem. My solution will be to write and reading operation on a .txt file on every seconds of timer


7 Years
Discussion Span
Last Post by Medalgod

You can open a file that is in use by another process, you just have to do it slightly different than you normally would:

//This gives us access to files that are in use by IIS -- sk
      using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (StreamReader sr = new StreamReader(fs))

          string line;
          int lineNumber = 0;
          while ((line = sr.ReadLine()) != null)
            if (string.IsNullOrEmpty(line))

However this sounds like a design flaw. You should probably lock the file handle and load the file contents in to a memory buffer, unless the file is large. I can't imagine a situation where you would want concurrent reads and writes to the same file so could you explain more of what you're doing? And as danny pointed out, post the code too.


it could be solved either by saving ur data in different place like in memory stream and this what u will depend on in reading process
and still u can write to the txt file

another solution could be closing the stream with each step

try and tell us what u got
good luck


The key here is really that you say you are reading data for another purpose. It is more manageable to have one file hold data for one thing.

If you are certain you want to have both sets of data in one file sknake's post contains all you need :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.