好几没来了.有来求助,关于操作注册表.
先上代码:
public partial class MainForm: Form {
private List<string> _DeleteResult = new List<string>(); //存储删除列表.
public MainForm() {
InitializeComponent();
labSearch.Click += (s, e) => { txtSearch.Focus(); };
}
private void Form1_Load(object sender, EventArgs e) {
combox_path.SelectedIndex = 0; //默认选第一项.
}
void RecursiveRegedit(RegistryKey regBoot) {
if(regBoot == null) throw new ArgumentNullException("项为空!");
string[] vals = regBoot.GetValueNames();
foreach(var v in vals) {
string s = regBoot.GetValue(v).ToString();
//以路径开头(不区分大小写)并匹配待查找字符串(不区分大小写).
if(s.StartsWith(combox_path.Text.Trim(), StringComparison.CurrentCultureIgnoreCase) && s.ToLower().Contains(txtSearch.Text.ToLower().Trim())) {
_DeleteResult.Add(v + "->" + s); //添加键值.
regBoot.DeleteValue(v, false);
}
}
if(regBoot.SubKeyCount <= 0)
return; //递归出口.
else {
string[] subs = regBoot.GetSubKeyNames();
foreach(string s in subs) {
RecursiveRegedit(regBoot.OpenSubKey(s, true));
}
}
regBoot.Close(); //关闭.
}
private void btn_Click(object sender, EventArgs e) {
if(MessageBox.Show("确定要删除?", "删除注册表", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) != DialogResult.Yes)
return;
if(!chebox_current_user.Checked && !chebox_local_machine.Checked) {
MessageBox.Show("请选择"删除选项"!");
return;
}
if(chebox_hasESRI.Checked) { //有ESRI项时仅扫描ESRI.
if(chebox_current_user.Checked)
RecursiveRegedit(Registry.CurrentUser.OpenSubKey("SOFTWARE").OpenSubKey("ESRI", true));
if(chebox_local_machine.Checked)
RecursiveRegedit(Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ESRI", true));
}
else {
if(chebox_current_user.Checked) //无ESRI项时扫描Software下所有.
RecursiveRegedit(Registry.CurrentUser.OpenSubKey("SOFTWARE", true));
if(chebox_local_machine.Checked)
RecursiveRegedit(Registry.LocalMachine.OpenSubKey("SOFTWARE", true));
}
MessageBox.Show("Done!共删除\t" + _DeleteResult.Count + "\t个");
foreach(var item in _DeleteResult) {
lbox.Items.Add(item);
}
}
}
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = @"HKEY_CURRENT_USER\Software;HKEY_LOCAL_MACHINE\Software;HKEY_USERS")]