I need to know when a text file that’s opened by C# is not in use anymore – My app writes text to a txt file and sometimes it will still have a lock on it when trying to write to it again.
There is no concern that another process or word may grab the lock back after it is released.
Is there a way to determine if the file is still locked and to reattempt the write if its not locked?
I need to know when a text file that’s opened by C# is not
Share
Below is an example of reading a file using a
usingblock, which will close the file for you:Some people prefer to omit the outer level of the braces when there are consecutive
usingblocks. I have included them for clarity. Note that ausingblock uses atry-finallyblock under the covers to ensure that your file will close even if there is an uncaught exception. This is a good thing.See also RAII.