C # TextWriter, allow reading files:

using (TextWriter writer = File.CreateText(path2)) { writer.Write(SomeText); } 

This is a problematic piece of code. When I write to a file, this is normal until another application opens the file. Then I get an error message.

How to write files that can be read at the same time?

+7
source share
1 answer

You need to specify FileShare.Read :

 using (Stream stream = File.Open(path2, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) using (TextWriter writer = new StreamWriter(stream)) { writer.Write(SomeText); } 

This will allow other processes to open the file for reading rather than writing.

+13
source

All Articles