c# open file with default application and parameters

The most easy way to open a file with the default application is:

System.Diagnostics.Process.Start(@"c:\myPDF.pdf");

However, I would like to know if exists a way to set parameters to the default application, because I would like to open a pdf in a determinate page number.

I know how to do it creating a new process and setting the parameters, but this way I need to indicate the path of the application, and I would like to have a portable application and not to have to set the path of the application each time I use the application in other computer. My idea is that I expect that the computer has installed the pdf reader and only say what to page open.

Thanks.


Solution 1:

If you want the file to be opened with the default application, I mean without specifying Acrobat or Reader, you can't open the file in the specified page.

On the other hand, if you are Ok with specifying Acrobat or Reader, keep reading:


You can do it without telling the full Acrobat path, like this:

using Process myProcess = new Process();    
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" C:\\example.pdf";
myProcess.Start();

If you don't want the pdf to open with Reader but with Acrobat, chage the second line like this:

myProcess.StartInfo.FileName = "Acrobat.exe";

You can query the registry to identify the default application to open pdf files and then define FileName on your process's StartInfo accordingly.

Follow this question for details on doing that: Finding the default application for opening a particular file type on Windows

Solution 2:

this should be close!

public static void OpenWithDefaultProgram(string path)
{
    using Process fileopener = new Process();

    fileopener.StartInfo.FileName = "explorer";
    fileopener.StartInfo.Arguments = "\"" + path + "\"";
    fileopener.Start();
}