How do I read and write on a text file without getting the exception that “File is already in use by another app”??
I tried File.readalltext() and File.Appendalltext() functions..I’m just starting out with filestream.
Which would work out best in my scenario? I would appreciate some code snipplets too ..
Thanks
This is all to do with the lock and sharing semantics that you request when opening the file.
Instead of using the shortcut approach of
File.ReadAllText(), try looking into using aSystem.IO.FileStreamand aSystem.IO.StreamReader/System.IO.StreamWriter.To open a file:
Note the
FileShare.ReadWrite– this is telling the stream to allow sharing to either other readers or other writers.For writing try something like
Note the
FileShare.Read– this is telling the stream to allow sharing to readers only.Have a read around the
System.IO.FileStreamand its constructor overloads and you can tailor exactly how it behaves to suit your purpose.