C# Open File with Default Application and Parameters

c# open file with default application and parameters

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

C# Open File With Associated Application passing arguments

I believe this is expected. Behind the scenes, Windows is finding the default application in the registry and creating a new process and passing your file name to it. I get the same behavior if I go to a command prompt and type "filename.ext argument", that my arguments are not passed to the application.

What you probably need to do is find the default application yourself by looking in the registry. Then you can start that process with arguments, instead of trying to start by filetype association. There is an answer here on how to find the default application in the registry:

Finding the default application for opening a particular file type on Windows

Open file with associated application

Just write

System.Diagnostics.Process.Start(@"file path");

example

System.Diagnostics.Process.Start(@"C:\foo.jpg");
System.Diagnostics.Process.Start(@"C:\foo.doc");
System.Diagnostics.Process.Start(@"C:\foo.dxf");
...

And shell will run associated program reading it from the registry, like usual double click does.

How to launch default application for an arbitrary file in .net?

Just use Process.Start (MSDN-article)

Process.Start(pathToYourFile);

Windows will launch the appropriate application. Just watch out: Up until Windows 8, this will throw an exception, if there is no standard application for that file type.

Open a file not using default program

You can use the System.Diagnostics.Process(String, String) method that you can find further documentation here

Sample:

// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");

For future posts, I suggest you post code that you have already attempted/written to help us better help you.

Open file with application

So here is what I did:

I made my own file extension .xxx (inside there is just plain xml) when I open it with my application I check in constructor of startup window for the parameters count using the

Environment.GetCommandLineArgs()

If count > 1 I get the second parameter which is string with absolute path to the file. Then I fetch the string to the method handling the opening of the file.



Related Topics



Leave a reply



Submit