-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.cs
139 lines (125 loc) · 3.42 KB
/
Registry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace PaJaMa.Common
{
public class Registry
{
public static Dictionary<string, string> GetOpenWithList(string extension)
{
if (!extension.StartsWith("."))
extension = "." + extension;
string baseKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + extension;
Dictionary<string, string> progs = new Dictionary<string, string>();
using (RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(baseKey + @"\OpenWithList"))
{
if (rk != null)
{
string mruList = (string)rk.GetValue("MRUList");
if (mruList != null)
{
foreach (char c in mruList.ToString())
{
string str = rk.GetValue(c.ToString()).ToString();
if (str.ToLower().Contains("dllhost")) continue;
var appPath = GetRegisteredApplication(str);
if (string.IsNullOrEmpty(appPath)) continue;
var appName = appPath;
var match = Regex.Match(appPath, "\"(.*?)\"");
if (match.Success)
appName = GetMuiCacheApplicationName(match.Groups[1].Value);
else
appName = GetMuiCacheApplicationName(appPath);
if (!progs.ContainsKey(appPath))
{
progs.Add(appPath, appName);
}
}
}
}
}
return progs;
}
public static string GetRegisteredApplication(string app)
{
//
// Return registered application by file's extension
//
RegistryKey oHKCR;
RegistryKey oOpenCmd;
string command;
if (Environment.Is64BitOperatingSystem == true)
{
oHKCR = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry64);
}
else
{
oHKCR = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry32);
}
try
{
oOpenCmd = oHKCR.OpenSubKey(app + "\\shell\\open\\command");
if (oOpenCmd == null)
{
oOpenCmd = oHKCR.OpenSubKey("\\Applications\\" + app + "\\shell\\open\\command");
}
if (oOpenCmd == null)
{
oOpenCmd = oHKCR.OpenSubKey("Applications\\" + app + "\\shell\\open\\command");
}
if (oOpenCmd == null)
{
oOpenCmd = oHKCR.OpenSubKey("\\Applications\\" + app + "\\shell\\edit\\command");
}
if (oOpenCmd == null)
{
oOpenCmd = oHKCR.OpenSubKey("Applications\\" + app + "\\shell\\edit\\command");
}
if (oOpenCmd != null)
{
command = oOpenCmd.GetValue(null).ToString();
oOpenCmd.Close();
}
else
{
return null;
}
}
catch
{
return null;
}
return command;
}
public static string GetMuiCacheApplicationName(string appPath)
{
RegistryKey oHKCR;
string appName = string.Empty;
if (Environment.Is64BitOperatingSystem == true)
{
oHKCR = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry64);
}
else
{
oHKCR = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.ClassesRoot, RegistryView.Registry32);
}
try
{
var appSubKey = oHKCR.OpenSubKey("Local Settings\\Software\\Microsoft\\Windows\\Shell\\MuiCache\\");
var valKey = appSubKey.GetValueNames().FirstOrDefault(vn => vn == appPath);
if (valKey != null)
appName = appSubKey.GetValue(valKey).ToString();
}
catch
{
return null;
}
return appName;
}
}
}