This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegistryHelper.cs
108 lines (105 loc) · 4.77 KB
/
RegistryHelper.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace VanSwitch
{
public static class RegistryHelper
{
public static bool RemoveRegistryValue(string fullPath,RegistryHive registryHive = RegistryHive.CurrentUser, RegistryView registryView = RegistryView.Default)
{
try
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Trying to delete {fullPath} registry value");
RegistryKey localKey = RegistryKey.OpenBaseKey(registryHive, registryView);
string keyName = Path.GetDirectoryName(fullPath).Replace("Computer\\", "");
string valueName = Path.GetFileName(fullPath);
using (RegistryKey key = localKey.OpenSubKey(keyName, true))
{
if (key != null)
{
key.DeleteValue(valueName);
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Deleted registry value of: {fullPath}");
return true;
}
}
return false;
}
catch (System.Security.SecurityException)
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Error while deleting {fullPath} value, not enough rights");
return false;
}
catch (Exception ex)
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Error while deleting {fullPath} value:\n{ex.StackTrace}");
return false;
}
}
public static bool SetRegistryValue(string fullPath, string value, RegistryHive registryHive = RegistryHive.CurrentUser, RegistryView registryView = RegistryView.Default)
{
try
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Trying to set {fullPath} registry value");
RegistryKey localKey = RegistryKey.OpenBaseKey(registryHive, registryView);
string keyName = Path.GetDirectoryName(fullPath).Replace("Computer\\", "");
string valueName = Path.GetFileName(fullPath);
using (RegistryKey key = localKey.OpenSubKey(keyName, true))
{
if (key != null)
{
key.SetValue(valueName, value);
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Set registry value of: {fullPath}");
return true;
}
}
return false;
}
catch (System.Security.SecurityException)
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Error while setting {fullPath} value, not enough rights");
return false;
}
catch (Exception ex)
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Error while setting {fullPath} value:\n{ex.StackTrace}");
return false;
}
}
public static string GetRegistryValue(string fullPath, RegistryHive registryHive = RegistryHive.CurrentUser, RegistryView registryView = RegistryView.Default)
{
RegistryKey localKey = RegistryKey.OpenBaseKey(registryHive, registryView);
string value = "";
try
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Trying to find {fullPath} registry value");
string keyName = Path.GetDirectoryName(fullPath).Replace("Computer\\", "");
string valueName = Path.GetFileName(fullPath);
using (RegistryKey key = localKey.OpenSubKey(keyName))
{
if (key != null)
{
value = key.GetValue(valueName).ToString();
if(value == null)
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Registry value of: {fullPath} is empty");
else
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Registry value of: {fullPath} = {value}");
return value;
}
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Key {keyName} = null");
return value;
}
}
catch (Exception ex)
{
Debug.WriteLine($"VanSwitch (RegistryHelper) : " + $"Error ocurred while searching for registry value of {fullPath}:\n{ex.StackTrace}");
return value;
}
}
}
}