0

I am learning C# and opening a text file using the following code.

 string text = System.IO.File.ReadAllText(@"\\127.0.0.1\Temp\players.txt");

The same text file is also being opened by other applications also and sometimes the application throws exception when the file is already opened by the other application.

I want to wait till the file is closed and than open it

Summit
  • 2,112
  • 2
  • 12
  • 36
  • 2
    Simply, you can't prevent this type of exceptions. You have to catch them and retry. – Alejandro Apr 05 '21 at 04:04
  • 3
    Do you see this question with answers? https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – Maxim Apr 05 '21 at 04:06
  • 1
    If a file/resource has already a lock, you can't obtain a new lock on it before the previous lock is being released. – Anton Kovachev Apr 05 '21 at 11:04

0 Answers0