以下是查询注册表信息的代码:
查找注册表中某后缀文件执行程序的代码
1 private RegistryKeyStatus GetExePath(string suffix, ref string exePath)
2 {
3 RegistryKey regSuffixKey = Registry.ClassesRoot.OpenSubKey(suffix);
4 if (regSuffixKey == null || regSuffixKey.GetValue("") == null)
5 return RegistryKeyStatus.NonAvailable;
6 string playerKey = regSuffixKey.GetValue("").ToString();
7 RegistryKey regPlayerKey = Registry.ClassesRoot.OpenSubKey(playerKey + "\\shell\\open\\command");
8 if (regPlayerKey == null)
9 return RegistryKeyStatus.RegKeyNotFound;
10 string[] keys = regPlayerKey.GetValue("").ToString().Split('"');
11 foreach (string key in keys)
12 {
13 if (!string.IsNullOrEmpty(key.Trim()))
14 {
15 exePath = key;
16 break;
17 }
18 }
19 return RegistryKeyStatus.Available;
20 }
21 public enum RegistryKeyStatus
22 {
23 Available = 0,//可用
24 NonAvailable = 1,//无可用
25 RegKeyNotFound = 2,//未找到注册键
26 }
2 {
3 RegistryKey regSuffixKey = Registry.ClassesRoot.OpenSubKey(suffix);
4 if (regSuffixKey == null || regSuffixKey.GetValue("") == null)
5 return RegistryKeyStatus.NonAvailable;
6 string playerKey = regSuffixKey.GetValue("").ToString();
7 RegistryKey regPlayerKey = Registry.ClassesRoot.OpenSubKey(playerKey + "\\shell\\open\\command");
8 if (regPlayerKey == null)
9 return RegistryKeyStatus.RegKeyNotFound;
10 string[] keys = regPlayerKey.GetValue("").ToString().Split('"');
11 foreach (string key in keys)
12 {
13 if (!string.IsNullOrEmpty(key.Trim()))
14 {
15 exePath = key;
16 break;
17 }
18 }
19 return RegistryKeyStatus.Available;
20 }
21 public enum RegistryKeyStatus
22 {
23 Available = 0,//可用
24 NonAvailable = 1,//无可用
25 RegKeyNotFound = 2,//未找到注册键
26 }
执行的话,只需要调用Process.Start()就可以了。
1 RegistryKeyStatus regKeyStatus = GetExePath(suffix, ref exePath);
2 Process.Start(exePath, "\"" + filePath + "\"");
注:上面的"\""一定要加,否则地址中有空格就可能找不到相应文件。
--小小的天,有大大的梦想!