1

Possible Duplicate:
How to both Read/Write File in C#

I want to write and read text to and from the same text file. Here is the code

TextWriter notesWriter = new StreamWriter("text.txt");
TextReader notesReader; = new StreamReader("text.txt"); 

Once the file is open for writing it is being locked and prevents to read from it. So, some exception is thrown like,

The process cannot access the file 'text.txt' because it is being used by another process.

what is the workaround for this ? thank you.

EDIT: Suppose, if want to change text in a textbox and save it in a file and read from it when required to set the text in the textbox.

Community
  • 1
  • 1
Sanjeev
  • 367
  • 1
  • 8
  • 20

5 Answers5

0

Every time you finish writing you should Close the file, unless you need to write AND read at the same time which is impossible something you should not do because not so safe/hard/useless in most cases with standard text files used to store data.

Gabber
  • 4,392
  • 5
  • 32
  • 47
  • `"unless you need to write And read at the same time which is impossible"` No it's not. – Servy Oct 16 '12 at 14:06
  • Yup, just red the future and edited before your comment (I knew it was coming :)) – Gabber Oct 16 '12 at 14:07
  • thread will be closed in a minute anyway – Servy Oct 16 '12 at 14:07
  • "something you should not do", again something I'd disagree with - as long as only one thread is using the file at any one time using the same file object is 100% a-ok as long as you are aware at what position in the file you are writing to. – Justin Oct 16 '12 at 14:08
  • I agree, but again, for simplicity, for flushing/buffering reasons usually opening a file for writing / reading at the same time is not allowed by standard IO libraries for high level languages. – Gabber Oct 16 '12 at 14:10
  • Also, as my answer states: *unless you need to write AND read at the same time* that implies more than one thread – Gabber Oct 16 '12 at 14:12
  • @Servy *closed as exact duplicate by...* I see you read the future too – Gabber Oct 16 '12 at 14:14
0

Try something like

using(TextWriter notesWriter = new StreamWriter("text.txt"))
{
//do write-things here
}

after the closing-braked the Streamwriter will be disposed and you can read the file.

Tomtom
  • 8,386
  • 7
  • 45
  • 83
0

The workaround is not to do it. While technically this can be done, the way you want to do it (by accessing the file using stream semantics) is almost impossible to be correct as, even if you fix the file sharing, it would imply you're reading back the same stuff you wrote in an infinite loop.

You can use a paging based file access metaphor, which again is very unlikely what you want to do.

The most likely option is that you want to write into a different file, a (modified?) copy of the original file, and then swap the copy with the original file.

Remus Rusanu
  • 273,340
  • 38
  • 408
  • 539
0

Sure you can read and write at the same time, but you only need one reference:

Stream l_fileStream = File.Open( "text.txt", FileMode.Open, FileAccess.ReadWrite );

http://msdn.microsoft.com/en-us/library/s67691sb.aspx

Now you can read/write to the file:

StreamReader l_reader = new StreamReader( l_fileStream );
StreamWriter l_writer = new StreamWriter( l_fileStream );

Why would you want to do this? I have no idea. Seems like you'd want to finish one operation before beginning the next, unless you want to get down and dirty in the actual byte array (like an advanced paging system), in which case you may not be quite at the experience level to pull such a thing off.

JDB still remembers Monica
  • 21,669
  • 4
  • 66
  • 107
0

You don't need to read and write at the same time, considering your edits.

  • Open the application
  • Read the file. Put the file's content in the textbox. Close the file
  • Save the textbox content into the file. Close the file.

As you can see, you never need to read and write at the same time if you close the file in between your uses.

Msonic
  • 1,448
  • 15
  • 25