1

I have been having trouble to figure out how to check in my program whether a pdf document is currently open or not. This is a major problem, because my program creates pdf documents for my client, but my client reviews her document by opening it and attempts to fix any spelling errors while the pdf document is open, causing the program to crash and lose a lot of progress. I have tried several methods to attempt in detecting whether the program is open or not, to no avail.

This program is a C# desktop application that is used to create pdf documents by using itextsharp.

Can somebody please give me a statement that i can use to check whether the pdf is open or not or being used by another process? My client cannot continue with her work until this is fixed.

  • Possible duplicate of [checking if pdf is password protected using itextsharp](https://stackoverflow.com/questions/11298651/checking-if-pdf-is-password-protected-using-itextsharp) – Anas Alweish Nov 09 '18 at 08:52
  • This could be a very related question[Is there a way to check if a file is in use?](https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) – Cleptus Nov 09 '18 at 09:13
  • If the PDF being locked by a different program causes your *program to crash and lose a lot of progress*, you apparently do something wrong. Consider catching the matching exceptions and handling such situations gracefully. – mkl Nov 09 '18 at 10:38
  • I have tried the above way to check if a file is in use and it doesn't work. Keeps returning the same bool, even if the file is open or not. Does this have something to do with MS Edge browser? Also, yes exceptions will make it easier to save progress – Thys Wentzel Nov 09 '18 at 11:50

2 Answers2

1

You should do it as you would do it for any type of file.

protected virtual bool IsFileInUse(FileInfo file)
{
    try
    {
          FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
          stream.Dispose();
    }
    catch (IOException)
    {
       //Could not access because file is in use.
        return true;
    }


//file is not in use
return false;
}

FileShare.None guarantees that while you have the access, no one else can open the file for modifying.

Note: This might not work if a reader has loaded the file stream and then closed the file. But then I guess it is not a problem for you.

Code Name Jack
  • 2,856
  • 24
  • 40
  • I am opening the pdf file on microsoft edge. I also tested this code and it always returns that the file is not in use, while the program crashes when i try to overwrite it while it is open in microsoft edge. Im using a simple if statement. Does microsoft edge have an influence on this? – Thys Wentzel Nov 09 '18 at 10:17
0

This is a method that I have used in my projects before to make sure if a file is in use:

 bool IsAvailable(string path)
 {
      try
      {
           if (File.Exists(path))
              using (File.OpenRead(path))
              {
                  return true;
              }
           else
              return false;
       }
       catch (Exception)
       {
           Thread.Sleep(100);
           return InUse(path);
       }
 }

It waits till file becomes avaiable (not in use). if in catch you return false instead recursive calling the method again it will just check it one time.

Also the code above would return false if file doesn't exist. So if you don't need that can remove that part too:

 bool IsAvailable(string path)
 {
      try
      {
          using (File.OpenRead(path))return true;
      }
      catch {}
      return false;
 }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • How can i test this code? It returns true or false, so i am supposed to be able to give it a path and then be able to use it in an if statement, such as: if(InUse(path)) { MessageBox.Show("File is currently open."); } As i run it above, it always returns true. Also, i open the pdf through microsoft edge, that might influence it. I just need a method of checking whether the program is being used by another process – Thys Wentzel Nov 09 '18 at 10:08
  • @ThysWentzel yes give it the path of file and get the result if it is in use or not, but actually method name and return value didn't match so i changed it to isavailable, you might also change the trues to falses and falses to trues. – Ashkan Mobayen Khiabani Nov 09 '18 at 10:10