2

I'm using PDFSharp to create a PDF. In their examples, they save a PDF and then then they start a process to choose your viewer to open it. It looks like this:

document.Save(fileName);
Process.Start(fileName);    

So in my testing, I realized that if Acrobat Reader is already open, I get an i/o exception because the process is already running. So I tried following this post: Detecting a Process is already running in windows using C# .net

about detecting the process. So I changed the above code to this:

        document.Save(fileName);
        if (System.Diagnostics.Process.GetProcessesByName("AcroRd32.exe").Length == 0)
        {
            Process.Start(fileName);    
        }

So I have two questions.
1) This doesn't work. The Length is always 0 so I'm wondering if I am returning the wrong process or it can't find the process. When I look in Task Manager, that AcroRd32.exe is the name of the process that is being run.

2) Is there a better way to do this? It seems like I'm hardcoding this process into the code and I wasn't sure if there was a better way to catch either other versions of Acrobat (like if there was a 64-bit version), or other PDF viewers in general.

Sorry if this is noob question. This .NET is pretty new to me. Thanks.

Community
  • 1
  • 1
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • you can look at this link by checking the Registry http://stackoverflow.com/questions/969027/check-adobe-reader-is-installed-c – MethodMan Jan 11 '13 at 00:30
  • >The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path. http://msdn.microsoft.com/en-us/library/z3w4xdc9.aspx – John Koerner Jan 11 '13 at 00:31
  • CPU will be at 50% or more. [Sorry, couldn't resist] – Gary McGill Jan 11 '13 at 00:34
  • also instead of the `document.Save(fileName);` is there a `document.SaveAs(fileName);` method you can use instead..? – MethodMan Jan 11 '13 at 00:41

2 Answers2

3

This is a really good source, and goes over everything you are asking.

Here is a little sample code taken from the above link:

Process[] collectionOfProcess = Process.GetProcessesByName("AcroRd32");
            if (collectionOfProcess.Length >= 1)
            {
                Process acrProcess = collectionOfProcess[0];
                
                MessageBox.Show(acrProcess.MainWindowTitle);// file name of the which is opened.

                MessageBox.Show("Acrobet reader running");
            }

Additionally, I have seen times where a program cannot detect or "work" with a process that is above it in permissions. Make sure that adobe reader is not running under Admin privileges, and if it is make sure your program is too.

Hope this helps!

Community
  • 1
  • 1
FrostyFire
  • 3,212
  • 3
  • 29
  • 53
  • JabFreeware, doesn't hat code assume that the application is running..? what if it's not running..? correct me if I am reading this incorrectly.. – MethodMan Jan 11 '13 at 00:40
  • @DJKRAZE Yes, it will detect if its already running. This is what he wanted: "So in my testing, I realized that if Acrobat Reader is already open, I get an i/o exception because the process is already running. " – FrostyFire Jan 11 '13 at 00:42
  • @DJKRAZE the OP specifically asks for **Detect if Adobe Acrobat Reader on .NET is running** ... he does say *Running* rather than *Installed* for example, wich that would go as a duplicated to http://stackoverflow.com/q/969027/28004 – balexandre Jan 11 '13 at 00:43
  • 1
    +2 Cool JabFreeware just wanted to make sure I understood what the OP was really stating.. I think it has to deal more with the original opened doc not being Disposed properly he would need to show how that file is being created as well as opened.. – MethodMan Jan 11 '13 at 00:45
  • Was not doubting your answer I tested the same code and it returned 0 but Crystal's issues probably deals with what I was stating earlier in my previous comments.. – MethodMan Jan 11 '13 at 00:47
0

If you pass the file path of the PDF file you have just created to Process.Start instead of trying to launch Acrobat Reader, Windows will open the PDF with the default application associated with the .pdf file extension. You will not even need to know what reader they have installed. If no reader is installed, Windows will prompt the user to choose an application to open it with. This is a better option that launching Acrobat Reader directly as it will open the PDF file with the user's preferred reader.

If this is what you are already doing (which I suspect it is from the question), then you are most likely getting the IO exception because the generated PDF file is still open in PDFSharp or your code (i.e. something has it locked), meaning that Acrobat Reader cannot open it. Starting a process that is already running should not cause that error.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • Yes I get the error when the generated pdf is still open. So I'm trying to detect a way to not start that process. Do you have advice on how to do that? – Crystal Jan 11 '13 at 00:38
  • 2
    If you have just generated the PDF file then it can't be open in Acrobat Reader, so it must be the generation code that has a handle to it. I think you need to solve that problem. Can you show the code that generates the PDF file? – adrianbanks Jan 11 '13 at 00:40
  • OIC. I ran it again and it craps out on the document.Save(fileName) because a Process (for me Adobe reader) is already using it). So I guess I need to check if the PDF I saved is being used? – Crystal Jan 11 '13 at 00:43
  • I suspect that your code (or the PDFSharp library) still has a handle to the generated PDF file. You need to release this handle before something else (e.g. Acrobat Reader) can open it. – adrianbanks Jan 11 '13 at 00:46
  • Crystal you should also check to see if it's close or better yet see if it can be opened in SharedMode |ReadWrite mode as well – MethodMan Jan 11 '13 at 00:46