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.
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
How to open a file with the default associated program
You can use Desktop.getDesktop().open(File file)
. See the following question for other options: "[Java] How to open user system preffered editor for given file?"
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
How can you open a file with the program associated with its file extension?
You want to use the file to open as the file argument, not the parameter argument. No need to specify which program to use, ShellExecute
will look it up for you.
ShellExecute(0, 0, L"c:\\outfile.txt", 0, 0 , SW_SHOW );
By leaving the verb as NULL (0) rather than L"open"
, you get the true default action for the file type - usually this is open
but not always.
Related Topics
Count the Items from a Ienumerable<T> Without Iterating
Find If Current Time Falls in a Time Range
How to Mock Out the File System in C# for Unit Testing
Centering Controls Within a Form in .Net (Winforms)
Programmatically Add Column & Rows to Wpf Datagrid
Showing Difference Between Two Datetime Values in Hours
How Can a Word Document Be Created in C#
Select Multiple Items from a Datagrid in an Mvvm Wpf Project
Is Using a Mutex to Prevent Multiple Instances of the Same Program from Running Safe
How to Force All Referenced Assemblies to Be Loaded into the App Domain
Kill Some Processes by .Exe File Name
Remove HTML Tags from String Including &Nbsp in C#
Accessing MVC's Model Property from JavaScript
How to Post an Array of Complex Objects with JSON, Jquery to ASP.NET MVC Controller
Running Scripts in HTMLagilitypack
How to Get the Current Line Number
Get the Serial Number of Usb Storage Devices in .Net Core 2.1