你没有说你正在开发什么语言,但是你应该可以通过在shlwapi.dll上调用assocquerystring来做到这一点。
assocquerystring API 函数将返回文件关联数据,而无需手动潜入注册表并处理其中的恶魔。大多数语言都支持调用 Windows API,所以你应该很高兴。
更多信息可以在这里找到:
http://www.pinvoke.net/default.aspx/shlwapi.assocquerystring
这里:
http://msdn.microsoft.com/en-us/library/bb773471%28VS.85%29.aspx
编辑:一些示例代码:
private void SomeProcessInYourApp()
{
// Get association for doc/avi
string docAsscData = AssociationsHelper.GetAssociation(".doc"); // returns : docAsscData = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
string aviAsscData = AssociationsHelper.GetAssociation(".avi"); // returns : aviAsscData = "C:\\Program Files\\Windows Media Player\\wmplayer.exe"
// Get association for an unassociated extension
string someAsscData = AssociationsHelper.GetAssociation(".blahdeblahblahblah"); // returns : someAsscData = "C:\\Windows\\system32\\shell32.dll"
}
internal static class AssociationsHelper
{
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra,
[Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);
[Flags]
enum AssocF
{
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200
}
enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic
}
public static string GetAssociation(string doctype)
{
uint pcchOut = 0; // size of output buffer
// First call is to get the required size of output buffer
AssocQueryString(AssocF.Verify, AssocStr.Executable, doctype, null, null, ref pcchOut);
// Allocate the output buffer
StringBuilder pszOut = new StringBuilder((int)pcchOut);
// Get the full pathname to the program in pszOut
AssocQueryString(AssocF.Verify, AssocStr.Executable, doctype, null, pszOut, ref pcchOut);
string doc = pszOut.ToString();
return doc;
}
}