Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file with associated application

i want to ask for help with opening a file from c# app with associated app. I tried this:

      ProcessStartInfo pi = new ProcessStartInfo(file);
      pi.Arguments = Path.GetFileName(file);
      pi.UseShellExecute = true;
      pi.WorkingDirectory = Path.GetDirectoryName(file);
      pi.FileName = file;
      pi.Verb = "OPEN";
      Process.Start(pi);

or this:

      Process.Start(file);

where string file in both examples represents full path to the file trying to open. Now, everything is working well, except the (jpg) images with ACDSee app. Irfanview associations works well, MS office documents too. After trying to open the jpg image associated with acdsee it just runs the acdsee in the notification area and does not open the file.

I discovered, that in the registry CLASSES_ROOT for *.jpg images, there is an ACDSee.JPG value as associated app, and under this key there is in the shell->Open->Command a path:

"C:\Program Files\ACD Systems\ACDSee\ACDSee.exe" /dde

and I thing that this weird /dde is the reason, why i cannot open the file. I realized that in the same reg key shell->Open there is some DDEExec key entry with value [open("%1")]

For Irfan view or other checked app there is not a ddeexec, just the normal command like

"C:\Program Files (x86)\IrfanView\i_view32.exe" "%1"

that can be run from command line after swaping the %1 for file name, but I could not run the command from acdsee entry in the command line :(

So my question is, how can I set up the ProcessStartInfo object to ensure that it will run all the files as it would be in the explorer by doubleclick, the standards and this DDEExec ones? Is there something other like DDEExec that I shoul be aware of? thanks and sorry for my EN

UPDATE: because this question still gets upvotes, I want to clarify that accepted answer works. I only had problem with old version of ACDSee and not with the Process.Start command or with the jpg extension.

like image 351
Zavael Avatar asked Apr 16 '12 12:04

Zavael


People also ask

How do I open a file with a specific application?

To open a file with an application other than the default, right-click the file and select the application you want from the top of the menu. If you do not see the application you want, select Open With Other Application. By default, the file manager only shows applications that are known to handle the file.

What does it mean to associate a file?

(1) The relationship of one file to another based on the data it contains. (2) An established relationship between a file and the application used to open it. There are default file associations pre-configured in every operating system for all the common file types.


3 Answers

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.

like image 171
Tigran Avatar answered Oct 21 '22 19:10

Tigran


In .Net Core (as of v2.2) it should be:

new Process
{
    StartInfo = new ProcessStartInfo(@"file path")
    {
        UseShellExecute = true
    }
}.Start();

Related github issue can be found here

like image 31
Mehdi Dehghani Avatar answered Oct 21 '22 19:10

Mehdi Dehghani


This is an old thread but just in case anyone comes across it like I did. pi.FileName needs to be set to the file name (and possibly full path to file ) of the executable you want to use to open your file. The below code works for me to open a video file with VLC.

var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
    Arguments = Path.GetFileName(path),
    UseShellExecute = true,
    WorkingDirectory = Path.GetDirectoryName(path),
    FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe",
    Verb = "OPEN"
};
Process.Start(pi)

Tigran's answer works but will use windows' default application to open your file, so using ProcessStartInfo may be useful if you want to open the file with an application that is not the default.

like image 23
Tree Avatar answered Oct 21 '22 20:10

Tree