C# TextWriter, allow reading of files:
Asked Answered
P

1

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?

Pallua answered 3/9, 2012 at 15:36 Comment(1)
What error do you get?Gracchus
K
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.

Kmeson answered 3/9, 2012 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.