the access status of file writealllines

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



the access status of file writealllines



I am writing some text to file using file.WriteAllLines. I want that when the WriteAllLines is writing to the file. Both other process are not allowed to read or write the file. Is it the default behavior of WriteAllLines?





when some one actualy write the file, no one else can read/write it. This is default behavior of all modern OS (as far as I can say):
– vasily.sib
Aug 13 at 4:26





No, File.WriteAllLines() uses FileShare.Read, allowing other processes to read the file. The odds that this causes a sharing violation in those other processes are quite low. Most typically it will be WriteAllLines that will fail because those other processes already have the file opened and did not use FileShare.Write. They never do.
– Hans Passant
Aug 13 at 8:52





What will happen if WriteAllLines happen first, while it is still writing the ReadAllLines happen next?
– want_to_be_calm
Aug 13 at 9:09





@HansPassant if you are right my answer below is a "works on my machine" thing. How can I rule that out?
– Markus Deibel
Aug 14 at 6:37




1 Answer
1



Other processes are blocked from writing to the same file since internally the WriteAllLines method keeps a StreamWriter open on the file until all lines have been written.


WriteAllLines


StreamWriter



See the implementation detail in the C# language reference



[UPDATE]



Reading is not possible as well, as proven with this test, which throws an IOException in the readerThread:


IOException


readerThread


using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject3

[TestClass]
public class UnitTest1

[TestMethod]
public void TestMethod1()

string lines = new string[5000000];

for (int i = 0; i < 5000000; i++)

lines[i] = $"Line_i";


Thread writerThread = new Thread(() =>

File.WriteAllLines("C:\tmp\wal_test.txt", lines);
);

Thread readerThread = new Thread(() =>

File.ReadLines("C:\tmp\wal_test.txt");
);

writerThread.Start();
Thread.Sleep(100);
readerThread.Start();







How about the read access? Is it also be blocked?
– want_to_be_calm
Aug 13 at 5:09





Reading is blocked as well see updated answer.
– Markus Deibel
Aug 13 at 5:27





Thanks. Another question is, how about when I am reading a file, is that writing will be blocked? For my guessing, that should be true. If one is reading a file, and that file is being written. It will be corrupt in the content being read.
– want_to_be_calm
Aug 13 at 6:04





This depends what you use to read the file. If you set up the reader yourself you have several options (search for FileMode) to control the access. For the ReadAllBytes or ReadAllLines you can create a similar test to the one I showed above.
– Markus Deibel
Aug 13 at 6:40


ReadAllBytes


ReadAllLines






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard