-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
189 lines (159 loc) · 6.99 KB
/
Program.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
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
179
180
181
182
183
184
185
186
187
188
189
using Microsoft.Win32;
using System;
using System.Configuration;
using System.IO;
using System.Linq;
namespace Changeling
{
class Program
{
static void Main(string[] args)
{
string message;
if (args.Length < 2)
{
message = String.Format(
@"Changes the default program that opens files of specified extension without changing the current file extension icon
changeling.exe program/key extension/file
program/key specify a full path to the program that you want to assign
the file type to. ""<program>"" ""%1"" will be used as a
command for files of that type
alternatively, you can define full command in app.config
and pass the key of that command instead
extension/file specify an extension or a path to a file with the extension
you want to change the default program for
");
}
else
{
string program = args[0];
string extension = args[1];
string value = ConfigurationManager.AppSettings[program];
if (value != null)
program = value;
else
program = String.Format(@"""{0}"" ""%1""", program);
message = Switch(Path.GetExtension(extension), program);
}
if (message != null)
{
Console.WriteLine(message);
}
}
private static string Switch(string extension, string command)
{
string victim = "";
using (var winExplorerSettings = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts"))
{
using (var extensionSettings = winExplorerSettings.OpenSubKey(extension, false))
{
if (extensionSettings == null)
{
return String.Format("Extension '{0}' is not registered on windows explorer.", extension);
}
victim = (string)extensionSettings.GetValue(null, "");
if (String.IsNullOrWhiteSpace(victim))
{
using (var userChoiceSettings = extensionSettings.OpenSubKey("UserChoice", false))
{
if (userChoiceSettings != null)
victim = userChoiceSettings.GetValue("ProgId", victim).ToString();
}
}
if (String.IsNullOrWhiteSpace(victim))
{
using (var availableProgIds = extensionSettings.OpenSubKey("OpenWithProgIds", false))
{
if (availableProgIds != null)
{
victim = availableProgIds.GetValueNames().FirstOrDefault();
}
if (String.IsNullOrWhiteSpace(victim))
return String.Format("No ProgId registered for '{0}' on windows explorer.", extension);
}
}
}
using (var victimSettings = Registry.ClassesRoot.OpenSubKey(victim, true))
{
if (victimSettings == null)
return String.Format("Got lost trying to find ProgId '{0}'.", victim);
var commandSettings = victimSettings.OpenSubKey(@"shell\open\command", true);
if (commandSettings == null)
return String.Format("Unable to reassign default program for '{0}'.", extension);
var oldValue = commandSettings.GetValue(null, null);
if (Object.Equals(oldValue, command))
{
Console.WriteLine("Already switched");
return null;
}
commandSettings.SetValue(null, command);
Console.WriteLine("Changed to " + command);
int number = 1;
string prefix = "kicked_out_";
bool saved = false;
string[] usedNames = commandSettings.GetValueNames();
while (!saved)
{
bool nameNotFound = true;
string oldValueName = prefix + number;
foreach (var name in usedNames)
{
if (name == oldValueName)
{
nameNotFound = false;
var savedValue = commandSettings.GetValue(name, prefix);
saved = Object.Equals(oldValue, savedValue);
break;
}
}
if (nameNotFound)
{
commandSettings.SetValue(oldValueName, oldValue);
saved = true;
}
else
{
number++;
}
}
using (var openSettings = victimSettings.OpenSubKey(@"shell\open", true))
{
CopyRegistryKey(openSettings, "ddeexec", "_ddeexec");
openSettings.DeleteSubKeyTree("ddeexec", false);
}
}
return null;
}
}
private static void CopyRegistryKey(RegistryKey parentKey, string keyNameToCopy, string newKeyName)
{
using (RegistryKey sourceKey = parentKey.OpenSubKey(keyNameToCopy))
{
if (sourceKey == null)
return;
using (RegistryKey destinationKey = parentKey.CreateSubKey(newKeyName))
{
PerformRegistryCopy(sourceKey, destinationKey);
}
}
}
private static void PerformRegistryCopy(RegistryKey sourceKey, RegistryKey destinationKey)
{
foreach (string valueName in sourceKey.GetValueNames())
{
object objValue = sourceKey.GetValue(valueName);
RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
destinationKey.SetValue(valueName, objValue, valKind);
}
foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
{
using (RegistryKey
sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName),
destSubKey = destinationKey.CreateSubKey(sourceSubKeyName))
{
PerformRegistryCopy(sourceSubKey, destSubKey);
}
}
}
}
}