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

This is problematic piece of code. When I write to file, it's ok, until other app open the file. Then I get error.

How to write files that can be read in same time?

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
el ninho
  • 4,183
  • 15
  • 56
  • 77

1 Answers1

14

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);
}

It will allow other processes to open the file for reading, not for writing.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758