c# - Reading / Writing text file repeatedly / simultaneously -


How can I read and write a text file without getting an exception "File is already in use by another app" ? ?

I tried the file .readalltext () and file. Appendalltext () functions .. I'm just starting with the filestream.

Who will work best in my scenario? I also appreciate some code snippets ..

thanks

all this Some are requested to open the file with your lock and share words.

Instead of using the file's shortcut approach. , a System.IO.FileStream and System.IO.StreamReader / System.IO.StreamWriter .

To open a file:

  (var file stream = new file stream (@ "c: by using the Stream Streamer = New Streamer (File Stream)) \ MyFile ", FileMode.Open, FileAccess.Read, FileShare.ReadWrite) {Var someText = streamReader.ReadToEnd (); }  

Note FileShare.ReadWrite - This is telling the stream to allow other readers or other authors to share.

To write (using var streamstart = new streamwriter (var streamwriter = new streamwriter) by using

  (var filestream = new file stream (@ "c : \ MyFile ", FileMode.Create, FileAccess.Write, FileShare. Read)) stream {streamWriter.WriteLine (" some text ");  

Note FileShare. Read - This is just telling the stream to allow readers to share.

System.IO.FileStream and its constructor overloads and you can present it exactly according to your purpose.


Comments