Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
178 lines (166 loc) · 6.3 KB
/
Copy pathProgram.cs
File metadata and controls
178 lines (166 loc) · 6.3 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.IO;
using Microsoft.Win32;
namespace RegManager
{
/// <summary>
/// Enable or Disable Windows Explorer integration and auto startup.
/// </summary>
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length == 0) return;
Console.WriteLine("Beginning operations...");
bool addAutoStartup = false;
bool changeAutoStartup = false;
bool addExplorerIntegration = false;
bool changeExplorerIntegration = false;
string path = "";
foreach (string s in args)
{
switch (s)
{
case "0":
addAutoStartup = true;
changeAutoStartup = true;
break;
case "1":
addAutoStartup = false;
changeAutoStartup = true;
break;
case "2":
addExplorerIntegration = true;
changeExplorerIntegration = true;
break;
case "3":
addExplorerIntegration = false;
changeExplorerIntegration = true;
break;
default:
if (File.Exists(s))
{
path = s;
}
else
{
Console.WriteLine("Warning: Argument not supported!");
return;
}
break;
}
}
if (changeAutoStartup)
{
ChangeAutoStartUp(addAutoStartup, path);
}
if (changeExplorerIntegration)
{
ChangeExplorerIntegration(addExplorerIntegration, path);
}
Console.WriteLine("Operation completed.");
}
/// <summary>
/// Enable Windows Explorer integration.
/// </summary>
/// <param name="path">The path to the file that should be added to Windows Explorer integration.</param>
private static void EnableExplorerIntegration(string path)
{
if (File.Exists(path))
{
Registry.ClassesRoot.CreateSubKey(@"*\shell\DeadLock", true);
Registry.ClassesRoot.CreateSubKey(@"*\shell\DeadLock\command", true);
Registry.SetValue(@"HKEY_CLASSES_ROOT\*\shell\DeadLock\command", "", path + " %1");
Registry.ClassesRoot.CreateSubKey(@"Directory\shell\DeadLock", true);
Registry.ClassesRoot.CreateSubKey(@"Directory\shell\DeadLock\command", true);
Registry.SetValue(@"HKEY_CLASSES_ROOT\Directory\shell\DeadLock\command", "", path + " %1");
}
else
{
Console.Write("Warning: path does not exist!");
}
}
/// <summary>
/// Disable Windows Explorer integration.
/// </summary>
private static void DisableExplorerIntegration()
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"*\shell\DeadLock\command"))
{
if (key != null)
{
Registry.ClassesRoot.DeleteSubKeyTree(@"*\shell\DeadLock");
}
}
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"Directory\shell\DeadLock\command"))
{
if (key != null)
{
Registry.ClassesRoot.DeleteSubKeyTree(@"Directory\shell\DeadLock");
}
}
}
/// <summary>
/// Enable or disable Windows Explorer Integration.
/// </summary>
/// <param name="value">A boolean to indicate whether Windows Explorer integration should be enabled.</param>
/// <param name="path">The path to the file that should be added to Windows Explorer integration.</param>
private static void ChangeExplorerIntegration(bool value, string path)
{
if (value)
{
Console.WriteLine("Enabling Windows Explorer integration...");
EnableExplorerIntegration(path);
}
else
{
Console.WriteLine("Disabling Windows Explorer integration...");
DisableExplorerIntegration();
}
}
/// <summary>
/// Enable auto startup.
/// </summary>
/// <param name="path">The path that should be added to the registry in order to enable auto startup.</param>
private static void EnableAutoStartup(string path)
{
if (File.Exists(path))
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "DeadLock", path);
}
else
{
Console.WriteLine("Warning: path does not exist!");
}
}
/// <summary>
/// Disable auto startup.
/// </summary>
private static void DisableAutoStartup()
{
if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "DeadLock", "").ToString() == "") return;
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
{
key?.DeleteValue("DeadLock");
}
}
/// <summary>
/// Enable or disable auto startup.
/// </summary>
/// <param name="value">A boolean to indicate whether auto startup should be enabled.</param>
/// <param name="path">The path that should be added to the registry in order to enable auto startup.</param>
private static void ChangeAutoStartUp(bool value, string path)
{
if (value)
{
Console.WriteLine("Enabling Auto Startup...");
EnableAutoStartup(path);
}
else
{
Console.WriteLine("Disabling Auto Startup...");
DisableAutoStartup();
}
}
}
}