Is an Application Associated With a Given Extension?
c#, file-extension
Solution
I would recommend following the advice in David's answer BUT since you need to detect an association:
To check whether a file has an association you can use the native function `FindExecutable` which is basically what Windows Explorer uses internally... it gives a nice error code (`SE_ERR_NOASSOC`) if there is no association. Upon success it gives a path to the respective executable.
Thee `DllImport` for it is
[DllImport("shell32.dll")]
static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult);
Another option would be to walk the registry for example (not recommended since complex due to several aspets like WoW64 etc.):
The real association is stored in the key that `HKEY_CLASSES_ROOT\.pdf` points to - in my case `AcroExch.Document`, so we checkout`HKEY_CLASSES_ROOT\AcroExch.Document`. There you can see (and change) what command is going to be used to launch that type of file:
HKEY_CLASSES_ROOT\AcroExch.Document\shell\open\command
Problem
It is sometimes desirable to have your application open the default application for a file. For example, to open a PDF file you might use: ``` System.Diagnostics.Process.Start("Filename.pdf"); ``` To open an image, you'd just use the same code with a different filename: ``` System.Diagnostics.Process.Start("Filename.gif"); ``` Some extensions (.gif for example) just about always have a default handler, even in a base Windows installation. However, some extensions (.pdf for example) often don't have an application installed to handle them. In these cases, it'd be desirable to determine if an application is associated with the extension of the file you wish to open before you make the call to Process.Start(fileName). I'm wondering how you might best 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; } ```