Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an Application Associated With a Given Extension?

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;
}
like image 366
bopapa_1979 Avatar asked Mar 02 '12 20:03

bopapa_1979


People also ask

What is the extension of a file?

Extensions tell your computer which application created or can open the file and which icon to use for the file. For example, the docx extension tells your computer that Microsoft Word can open the file and to display a Word icon when you view it in File Explorer.

What is an extension name?

A filename extension, file name extension or file extension is a suffix to the name of a computer file (e.g., . txt , . docx , . md ). The extension indicates a characteristic of the file contents or its intended use.

What is an extension on a computer?

An extension, in computer operating systems, is a piece of software that enhances or "extends" the capabilities of a programming language or other applications. An extension adds extra features to an already working standalone application.


1 Answers

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 checkoutHKEY_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
like image 70
Yahia Avatar answered Oct 04 '22 00:10

Yahia