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

Dani AI

Generated

The exception you posted is the classic "file locked by another handle" problem: something is keeping e:\pctext.txt open so the writer or reader cannot get the access mode it needs. Several good directions already appear in the thread: pointed out allowing shared reads, suggested in-memory buffering, and suggested separating the data. Below are practical, durable options and quick troubleshooting steps.

A robust pattern is atomic replace: have the writer write a complete new copy to a temp file, then atomically replace the original. Readers only ever see the old or the new full file, never a half-written file. On Windows/.NET use File.Replace (or an atomic move on the same volume) so the swap is reliable. Example pattern:

File.WriteAllText(tempPath, contents);
File.Replace(tempPath, targetPath, null);

If you must keep the writer open and allow concurrent readers (the approach suggested), open the writer with a sharing mode that permits read access. That avoids the IOException but introduces the risk that a reader sees partially-written content. Mitigate that by writing an explicit commit marker or version number at the end of each update, so readers ignore incomplete entries. See the FileShare options for details: FileShare documentation.

If reader and writer are in the same application, prefer an in-memory queue (producer/consumer) to avoid disk locks entirely. For cross-process sharing without disk-lock problems, consider memory-mapped files or lightweight IPC. See MemoryMappedFile docs for options: MemoryMappedFile documentation.

Troubleshooting tips: always dispose streams with using, call Flush before replacing, add a short retry/backoff when catching IOException, and use Sysinternals Handle or Process Explorer to identify which process holds the lock: Sysinternals Handle. If updates are frequent and small, consider an append-log or a tiny embedded store (SQLite) instead of a shared flat file for better reliability.

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.