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

pardeep

Recommended Answers

All 4 Replies

Could you point out in your code where the error occurs?

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)
          {
            lineNumber++;
            if (string.IsNullOrEmpty(line))
              continue;

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.