0

I am having an issue where I am writing to a file at the same time other's are manually reading/writing to the file. I would like to lock the file so that all open instances become read-only once FileStream is called.

FileStream fsFile = new FileStream(stFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);

Will the FileShare.Read attribute prevent manual changes being saved if the text file is open PRIOR to FileStream being called?

Samantha
  • 81
  • 1
  • 1
  • 8
  • 3
    No, the filestream open will fail as the file is already being shared read/write and so the attempt to lock it to read-only will fail. – David Arno Oct 01 '13 at 17:08
  • 1
    duplicate -> http://stackoverflow.com/questions/1707196/how-can-i-lock-a-file-while-writing-to-it-via-a-filestream – gleng Oct 01 '13 at 17:08
  • @gleng: my question is more specifically about how to prevent changes to a file opened prior the FileStream open. Is there a way to change an open file from read/write to read-only? – Samantha Oct 01 '13 at 17:43
  • In my experience with this, i'd have to say no. If the file is opened before this is called, then I don't think it's possible to do what you're saying. – gleng Oct 01 '13 at 17:45

0 Answers0