How to create C# list of ALL file associations(friendly application names & executables) for file type
I want to create a list/dictionary of all the friendly application names and their executable paths & commands for a specific file type. I've used AssocQueryString but it only returns one instance value.
For example:
The file type ".cs开发者_JAVA百科" on my PC has got many application associations for it when I right-click on a ".cs" file and select "Open With->" in the context menu, like "Microsoft Visual Studio 2008" & "Microsoft Visual Studio 2010" & "Notepad" & "Wordpad".
How can I enumerate all these values into a list in C#, obviously it must be extracted from the registry, but I'm very lost in all the "OpenWithList" and "OpenWithProgids" keys..
Thanks
In HKEY_CLASSES_ROOT get the keys that start with "." Read their default value (REG_SZ) to get the name of the key to open next, Then to get the program location read in "key_name\shell\open\command" the default value
this gets you the default app associated with that file type.
It is similar with the OpenWithProgIds - each value name inside is the key name of the key you should get the path to
P.S. here is a code I just wrote, very messy, not safe and you'll have to clean up the call parameters to get the pure app location. This is just to get you started
List<string> GetRegAssociatedFiles(string FileType)
{
List<string> _ret = new List<string>();
Microsoft.Win32.RegistryKey _rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(FileType);
string _defaultapp = _rk.GetValue("").ToString();
Microsoft.Win32.RegistryKey _rkapp = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(_defaultapp + "\\shell\\open\\command");
_ret.Add(_rkapp.GetValue("").ToString());
_rkapp.Close();
string[] _subkeys = _rk.GetSubKeyNames();
for (int i = 0; i < _subkeys.Length; i++)
{
if (_subkeys[i] == "OpenWithProgIds")
{
Microsoft.Win32.RegistryKey _rkh = _rk.OpenSubKey(_subkeys[i]);
string[] _names = _rkh.GetValueNames();
for (int j = 0; j < _names.Length; j++)
{
if (_names[j] == "")
continue;
Microsoft.Win32.RegistryKey _rhelp = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(_names[j] + "\\shell\\open\\command");
_ret.Add(_rhelp.GetValue("").ToString());
_rhelp.Close();
}
}
}
_rk.Close();
return _ret;
}
精彩评论