locked
File.ReadAllText and File.WriteAllText RRS feed

  • Question

  • User1970040472 posted

    Hello,

    ASP.NET 4.5 Web Form

    I have some code like in a page...

    String content = File.ReadAllText("myfile.txt");

    if(content == null) {

           File.WriteAllText("myfile.txt","content");

    }

    // output content..

    I believe File.WriteAllText uses FileShare.Read..which means the file can be read during writing... Asp.net is muti-threaded environment..so a request could read the myfile.text partially....So for example, A request could be writing a content in myfile.txt...at the same time, different B request comes in..it could read the file partially because the file set to FileShare.Read option internally. Is my understanding correct?... then should I use Mutex in order to avoid this scenario? or better option? (P.s : I am looking for file-based solution, not db one BTW)

    Thanks,

    Monday, November 18, 2019 4:22 PM

All replies

  • User475983607 posted

    Web site are multi-user applications.  It is certainly possible for one request to read the text file while another is writing to the same file.  

    should I use Mutex in order to avoid this scenario?

    Yes, serialize access to the file.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement

    Monday, November 18, 2019 4:28 PM
  • User1970040472 posted

    Thanks,

    Is there anyway I can possibly simulate that scenario by writing a sort of test program?

    I guess I can create two threads, one is for reading(Say A thread) and another one is for writing(Say B thread)....but the tricky part I guess is how to run the A thread exactly the moment while B thread is running(during the time actually writing is happening) so I can see the partial data output? is this something possible to write such simulation test program you think?

    Thanks,

    Monday, November 18, 2019 7:36 PM
  • User475983607 posted

    Is there anyway I can possibly simulate that scenario by writing a sort of test program?

    I guess I can create two threads, one is for reading(Say A thread) and another one is for writing(Say B thread)....but the tricky part I guess is how to run the A thread exactly the moment while B thread is running(during the time actually writing is happening) so I can see the partial data output? is this something possible to write such simulation test program you think?

    Write a tight loop.

    Monday, November 18, 2019 7:44 PM
  • User1970040472 posted

    No..I don't think that would be possible. There would be no way to read partially. Since it uses FileShare.Read internally, it disallows anyone writing against it. So basically what it would happen is that "A process is being used by another processor" that you gonna see...well..obviously, in order to avoid this one, lock will be needed, but I don't think partial read scenario could happen here. Anyway, thanks for your comments though.

    Tuesday, November 19, 2019 4:53 PM
  • User475983607 posted

    No..I don't think that would be possible. There would be no way to read partially. 

    I'm a little confused.  Isn't that exactly what you are testing?  If the code is written correctly then you should be able to write a tight loop that does several concurrent requests at a time.  You should not receive errors if the code is written properly.

    Tuesday, November 19, 2019 5:06 PM
  • User1970040472 posted

    Sorry if I confused you, but what I was curious initially about "Partial Read"... which means...

    Let's say my content, "ABCDEFGHIJ"..Say A thread is writing this to the file.."ABC" ..and at this point, B thread is interleaving and read the file...so it returns "ABC" without an error...and then back to A thread writing the rest...which is "DEFGHIJ"... That was what I meant by "Partial Read"....Like I said, locking is obviously needed to handle the file correctly, but "Partial Read" couldn't happen because of the reason I mentioned earlier

    Tuesday, November 19, 2019 5:13 PM
  • User753101303 posted

    Hi,

    You could have a look at https://referencesource.microsoft.com/ to check the underlying implementation. Another option could be to use two VS instances and use the debugger so that on both sides you can control if you are at the open, read/write or close step and see how it behaves (but you won't be able to test this method or at least it would be a bit more complex likely than to just "reproduce" this).

    In short you want to make sure that nobody can write to the file you are reading to and handle possible locks ?

    Tuesday, November 19, 2019 5:31 PM