Is an Application Associated With a Given Extension?

断了今生、忘了曾经 提交于 2019-11-27 21:25:53
Yahia

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

In a situation like this the best approach is to try to open the document and detect failure. Trying to predict whether or not a file association is in place just leads to you reimplementing the shell execute APIs. It's very hard to get that exactly right and rather needless since they already exist!

@Yahia gets the nod. I'm posting my quick solution for posterity so you can see what I went with. There are lots of possible improvements to this code, but this will give you the 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);

You will have too look at the registry to get that information.

You can follow from:

HKEY_CLASSES_ROOT\.extension

and it usually leads to 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 may be ideal to just ask for forgiveness ( that is, just open the file and see)

All of that information lives in the registry.. you could navigate 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'll need to wade into CLSIDs and whatnot... you're probably better off catching an exception instead.

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!