144

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.

Lombas
  • 1,000
  • 1
  • 8
  • 24
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • Do you mean send parameters to the Adobe executable rather than the pdf file, but without using the full path? – Bali C Jul 06 '12 at 16:19
  • 3
    How do you expect this to work? If you do not know the path of the application, you do not know which is the default PDF viewer, and you don't know which parameter format to use. – ken2k Jul 06 '12 at 16:20
  • 2
    Isn't enough to say `myProcess.StartInfo.FileName = "Acrobat.exe";` without giving the full path to the application? – daniloquio Jul 06 '12 at 16:23
  • 1
    Take a look at this [SO question](http://stackoverflow.com/questions/619158/adobe-reader-command-line-reference) to see it helps – Mark Hall Jul 06 '12 at 16:25
  • 3
    @daniloquio: I think the point is that the OP doesn't know what the end user has installed on his/her machine to read adobe files with, it could be Acrobat, Acrobat Reader, or something else. – Surfbutler Jul 06 '12 at 16:28
  • many be this link will helps you http://stackoverflow.com/questions/5017221/c-sharp-open-file-with-associated-application-passing-arguments... – Glory Raj Jul 06 '12 at 16:36
  • It seems like you're presuming what the default application is, and that it even has a parameter for doing what you need. Suppose I have foxit: it specifies pages by `/n _xxx_`. That's different than adobe reader: `page=_xxx_`. If you're going with the default application, you can't make assumptions about which program the user's chosen as that default. – Reacher Gilt Jul 06 '12 at 17:04

5 Answers5

82

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();
}
TorbenJ
  • 4,462
  • 11
  • 47
  • 84
Jessie Lesbian
  • 1,273
  • 10
  • 14
61

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

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
daniloquio
  • 3,822
  • 2
  • 36
  • 56
  • 2
    +1 Also I think it's possible to look up the application associated with any file type e.g. '.pdf' in the registry, then put that name in the filename parameter. See http://stackoverflow.com/questions/162331/finding-the-default-application-for-opening-a-particular-file-type-on-windows?rq=1 – Surfbutler Jul 06 '12 at 16:38
  • Is there some open parameter to get Adobe reader to open in 2-page view? Just curious, if someone knows, that would be real useful. I've been looking around, but I can't find anything. – taki Martillo Nov 06 '14 at 16:02
  • There is a workaround here that doesn't require you to specify an executable. Simply invoke a command-line and it will figure out the executable for you. e.g. Process.Start("cmd /C test.pdf"). While maybe not appropriate for production software, this is a perfectly acceptable way to ensure the PDF will open on different machines; perhaps appropriate for an internal tool. – djrecipe Nov 12 '20 at 05:27
8

I converted the VB code in the blog post linked by xsl to C# and modified it a bit:

public static bool TryGetRegisteredApplication(
                     string extension, out string registeredApp)
{
    string extensionId = GetClassesRootKeyDefaultValue(extension);
    if (extensionId == null)
    {
        registeredApp = null;
        return false;
    }

    string openCommand = GetClassesRootKeyDefaultValue(
            Path.Combine(new[] {extensionId, "shell", "open", "command"}));

    if (openCommand == null)
    {
        registeredApp = null;
        return false;
    }

    registeredApp = openCommand
                     .Replace("%1", string.Empty)
                     .Replace("\"", string.Empty)
                     .Trim();
    return true;
}

private static string GetClassesRootKeyDefaultValue(string keyPath)
{
    using (var key = Registry.ClassesRoot.OpenSubKey(keyPath))
    {
        if (key == null)
        {
            return null;
        }

        var defaultValue = key.GetValue(null);
        if (defaultValue == null)
        {
            return null;
        }

        return defaultValue.ToString();
    }
}

EDIT - this is unreliable. See Finding the default application for opening a particular file type on Windows.

Community
  • 1
  • 1
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
2

you can try with

Process process = new Process();
process.StartInfo.FileName = "yourProgram.exe";
process.StartInfo.Arguments = ..... //your parameters
process.Start();
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
-8

Please add Settings under Properties for the Project and make use of them this way you have clean and easy configurable settings that can be configured as default

How To: Create a New Setting at Design Time

Update: after comments below

  1. Right + Click on project
  2. Add New Item
  3. Under Visual C# Items -> General
  4. Select Settings File
HatSoft
  • 11,077
  • 3
  • 28
  • 43