C # open file with application and default settings

The easiest way to open a file with a standard application:

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

However, I would like to know if there is a way to set the parameters for the application by default, because I would like to open the PDF in a specific page number.

I know how I can do this by creating a new process and setting parameters, but in this way I need to specify the path to the application, and I would like to have a portable application and not set the application path every time I use the application on another computer. My idea is that I expect that the computer has installed a PDF reader and will only tell which page is open.

Thank.

+88
c # file
Jul 06 '12 at 16:15
source share
5 answers

EDIT (thanks to the surfbutler comment in the comments on the question) If you want the file to be opened by the default application, I mean without specifying Acrobat or Reader, you cannot open the file on the specified page.

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




You can do this without specifying the full Acrobat path, for example:

 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 do not want the PDF file to be opened using Reader, but with Acrobat, run the second line as follows:

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

SECOND EDIT: Search for the default PDF extension application

You can query the registry to determine the default application to open PDF files, and then define the FileName in your StartInfo process accordingly. Again, thanks surfbutler for your comment :)

Follow this question for more information on this: Finding a default application to open a specific file type in Windows

+37
Jul 06 '12 at 16:25
source share

I converted the VB code to a blog post related xsl to C # and changed 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 is unreliable. See Search for a default application to open a specific file type in Windows .

+7
Jul 21 '13 at 14:47
source share

you can try with

 Process process = new Process(); process.StartInfo.FileName = "yourProgram.exe"; process.StartInfo.Arguments = ..... //your parameters process.Start(); 
+3
Jul 06 '12 at 16:18
source share

it should be close!

 public static void OpenWithDefaultProgram(string path) { Process fileopener = new Process(); fileopener.StartInfo.FileName = "explorer"; fileopener.StartInfo.Arguments = "\"" + path + "\""; fileopener.Start(); } 
+2
Jan 20 '19 at 9:27
source share

Please add “Settings” to the “Properties for the project” and use them in such a way that you have clean and simple customizable parameters that can be set by default.

How to create a new setting during development

Update: after comments below

  • Right click on the project
  • Add New Item
  • Under "Visual C # Elements" → "General"
  • Select a settings file
-5
Jul 06 2018-12-12T00:
source share



All Articles