Is the application associated with this extension?

It is sometimes desirable for your application to open the default application for the file. For example, to open a PDF file that you can use:

System.Diagnostics.Process.Start("Filename.pdf"); 


To open an image, you simply use the same code with a different file name:

 System.Diagnostics.Process.Start("Filename.gif"); 


Some extensions (e.g. .gif) always have a default handler, even with a basic Windows installation. However, some extensions (e.g. .pdf) often do not have an application for processing them.

In these cases, it would be advisable to determine whether the application is associated with the file extension that you want to open before making a call to Process.Start (file_name).

I am wondering what is the best way to implement something like this:

 static bool ApplicationAssociated(string extension) { var extensionHasAssociatedApplication = false; var condition = // Determine if there is an application installed that is associated with the provided file extension.; if (condition) { extensionHasAssociatedApplication = true; } return extensionHasAssociatedApplication; } 
+12
c # file-extension
Mar 02 2018-12-12T00:
source share
6 answers

I would advise you to follow the David answer recommendations, but since you need to find a connection:

To check if a file has a link, you can use the built-in FindExecutable function, which is mainly used by Windows Explorer. it gives a good error code ( SE_ERR_NOASSOC ) if there is no connection. After success, it gives the path to the corresponding executable file.

Those DllImport for him

 [DllImport("shell32.dll")] static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult); 

Another option is, for example, to walk through the registry (not recommended, since it is complicated due to several aspets, such as WoW64, etc.):

The actual association is stored in the key that HKEY_CLASSES_ROOT\.pdf points to - in my case, AcroExch.Document , so we check HKEY_CLASSES_ROOT\AcroExch.Document . There you can see (and change) which command will be used to run this file type:

 HKEY_CLASSES_ROOT\AcroExch.Document\shell\open\command 
+25
Mar 02 2018-12-12T00:
source share

In such a situation, the best way is to try to open the document and detect a failure. Attempting to predict whether file associations exist simply results in re-execution of the shell APIs. It is very difficult to understand that this is correct and rather useless, since they already exist!

+5
Mar 02 '12 at 20:45
source share

@Yahia gets a nod. I am posting my quick solution for posterity so you can see what I went with. There are many possible improvements in this code, but this will give you an idea:

 public static bool HasExecutable(string path) { var executable = FindExecutable(path); return !string.IsNullOrEmpty(executable); } private static string FindExecutable(string path) { var executable = new StringBuilder(1024); FindExecutable(path, string.Empty, executable); return executable.ToString(); } [DllImport("shell32.dll", EntryPoint = "FindExecutable")] private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult); 
+4
Mar 02 2018-12-12T00:
source share

You can also view the registry to get this information.

You can follow from:

 HKEY_CLASSES_ROOT\.extension 

and usually this results in something like HKEY_CLASSES_ROOT\extfile\Shell\Open\Command

and you will come to the command to open the file type.

Depending on what you are doing, it might be ideal to just apologize (i.e. just open the file and see)

+1
Mar 02 2018-12-12T00:
source share

All this information lives in the registry .. you can go to HKEY_CLASSES_ROOT, find the extension and go from there to find the default handler. But depending on the type of file and the associated handler (s) you need to sneak into the CLSID and something else ... you are probably better off avoiding the exception.

+1
Mar 02 2018-12-12T00:
source share

This information is in the registry. For example:

 # Mount the HKCR drive in powershell ps c:\> new-psdrive hkcr registry hkey_classes_root ps c:\> cd hkcr:\.cs # get default key for .cs PS hkcr:\.cs> gp . "" (default) : VisualStudio.cs.10.0 ... # dereference the "open" verb PS hkcr:\.cs> dir ..\VisualStudio.cs.10.0\shell\open Hive: hkey_classes_root\VisualStudio.cs.10.0\shell\open Name Property ---- -------- Command (default) : "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" /dde ddeexec (default) : Open("%1") 
+1
Mar 02 2018-12-21T00:
source share



All Articles