-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
158 lines (141 loc) · 5.81 KB
/
Form1.cs
File metadata and controls
158 lines (141 loc) · 5.81 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
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Windows.Forms;
using System.Linq;
using Microsoft.Win32;
namespace FastMD5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!IsElevated())
{
try
{
RestartAsAdmin();
}
catch (Exception ex)
{
MessageBox.Show($"无法以管理员身份重新启动程序:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
try
{
string menuText = textBox1.Text.Trim();
if (string.IsNullOrEmpty(menuText))
{
MessageBox.Show("请输入有效的菜单名称!", "无效输入", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string targetPath = Path.Combine(userDir, "FastMD5.exe");
File.Copy(Application.ExecutablePath, targetPath, overwrite: true);
string menuFilePath = Path.Combine(userDir, "FastMD5_MenuName.ini");
File.WriteAllText(menuFilePath, menuText);
File.SetAttributes(menuFilePath, FileAttributes.Hidden);
using (RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true))
{
foreach (string subKeyName in shellKey.GetSubKeyNames())
{
if (subKeyName == menuText)
{
shellKey.DeleteSubKeyTree(subKeyName);
break;
}
}
using (RegistryKey key = shellKey.CreateSubKey(menuText))
{
key.SetValue("", menuText, RegistryValueKind.String);
key.SetValue("Icon", targetPath + ",0", RegistryValueKind.String);
using (RegistryKey commandKey = key.CreateSubKey("command"))
{
commandKey.SetValue("", $"\"{targetPath}\" \"%1\"", RegistryValueKind.String);
}
}
}
MessageBox.Show("右键菜单项已成功更新!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"添加菜单项失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (!IsElevated())
{
try
{
RestartAsAdmin();
}
catch (Exception ex)
{
MessageBox.Show($"无法以管理员身份重新启动程序:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
try
{
string userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string menuFilePath = Path.Combine(userDir, "FastMD5_MenuName.ini");
if (!File.Exists(menuFilePath))
{
MessageBox.Show("未找到菜单名称记录文件,无法确定要删除的菜单项。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string menuText = File.ReadAllText(menuFilePath).Trim();
using (RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true))
{
if (shellKey != null && shellKey.GetSubKeyNames().Contains(menuText))
{
shellKey.DeleteSubKeyTree(menuText);
MessageBox.Show($"右键菜单项 '{menuText}' 已成功移除!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"未找到名为 '{menuText}' 的右键菜单项。", "未找到菜单项", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
string targetPath = Path.Combine(userDir, "FastMD5.exe");
if (File.Exists(targetPath))
{
File.Delete(targetPath);
}
File.Delete(menuFilePath); // 删除菜单名称记录文件
}
catch (Exception ex)
{
MessageBox.Show($"删除操作失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool IsElevated()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private void RestartAsAdmin()
{
string exePath = Application.ExecutablePath;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = exePath,
Verb = "runas",
UseShellExecute = true
};
Process.Start(startInfo);
Application.Exit();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}