-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
138 lines (105 loc) · 3.67 KB
/
Config.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
using IniParser;
using IniParser.Model;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using System.Security.Principal;
namespace ProtocolHandler
{
class Config
{
public enum HIVE_KEYS { HKCU, HKLM, HKCR }
public static string REG_BASE = "Software\\fweber-de\\ProtocolHandler";
public string Protocol { get; set; }
public string HandlerPath { get; set; }
public string AdditionalArguments { get; set; }
public static Config LoadFromFile(string protocol)
{
string filename = $"config.{protocol}.ini";
if(!File.Exists(filename))
{
return null;
}
Config config = new Config();
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(filename);
config.Protocol = protocol;
config.HandlerPath = data["Handler"]["Path"];
config.AdditionalArguments = data["Handler"]["AdditionalArguments"];
return config;
}
public static Config LoadFromRegistry(string protocol)
{
Config config = new Config();
config.Protocol = protocol;
config.HandlerPath = (string)GetSettingFromRegistry(protocol, "HandlerPath");
config.AdditionalArguments = (string)GetSettingFromRegistry(protocol, "AdditionalArguments");
if(config.HandlerPath == "" || config.HandlerPath == null)
{
return null;
}
return config;
}
public static object GetSettingFromRegistry(string path, string key, string def = null)
{
string _base = REG_BASE;
if(path != "")
{
_base = _base + @"\" + path;
}
object value = Config.GetRegistryValue(HIVE_KEYS.HKCU, _base, key, def);
if (value != null)
{
return value;
}
value = Config.GetRegistryValue(HIVE_KEYS.HKLM, _base, key, def);
if (value != null)
{
return value;
}
return def;
}
public static void SetRegistryValue(HIVE_KEYS hiveKey, string keyPath, string key, object value, RegistryValueKind registryValueKind)
{
string hk = "";
if (hiveKey == HIVE_KEYS.HKCU)
{
hk = "HKEY_CURRENT_USER";
}
if (hiveKey == HIVE_KEYS.HKLM)
{
hk = "HKEY_LOCAL_MACHINE";
}
if (hiveKey == HIVE_KEYS.HKCR)
{
hk = "HKEY_CLASSES_ROOT";
}
Registry.SetValue(hk + "\\" + keyPath, key, value, registryValueKind);
}
public static object GetRegistryValue(HIVE_KEYS hiveKey, string keyPath, string key, string defaultValue = null)
{
string hk = "";
if (hiveKey == HIVE_KEYS.HKCU)
{
hk = "HKEY_CURRENT_USER";
}
if (hiveKey == HIVE_KEYS.HKLM)
{
hk = "HKEY_LOCAL_MACHINE";
}
if (hiveKey == HIVE_KEYS.HKCR)
{
hk = "HKEY_CLASSES_ROOT";
}
return Registry.GetValue(hk + "\\" + keyPath, key, defaultValue);
}
public static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
}