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.
This is a really good source, and goes over everything you are asking.
Here is a little sample code taken from the above link:
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!