C#简单注册表操作实例
1.简介操作
//设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径不存在,则创建 //2.如果指定键存在,则覆盖值 string path = "HKEY_CURRENT_USER\\myRegOne"; Registry.SetValue(path, "Expend", "hellow world!"); MessageBox.Show("设置成功"); } //获取指定路径的键对应的值 private void Button_Click_1(object sender, RoutedEventArgs e) { //如果不存在则报错 //1.如果指定路径不存在,获取的结果为null string path = "HKEY_CURRENT_USER\\myRegOne"; object result = Registry.GetValue(path, "Version", 1.0); lblOne.Content = result.ToString(); }
2.项操作:
//创建项 private void Button_Click(object sender, RoutedEventArgs e) { RegistryKey lm = Registry.CurrentUser; //打开myRegOne RegistryKey myRegOne = lm.OpenSubKey("myRegOne", true);//指定是否具有写权限 if (myRegOne != null) { //新建子项,如果存在则覆盖 RegistryKey mySubKey = myRegOne.CreateSubKey("mySubKey_Three", RegistryKeyPermissionCheck.ReadWriteSubTree); MessageBox.Show("创建成功!"); } else { myRegOne = lm.CreateSubKey("myRegOne", RegistryKeyPermissionCheck.ReadWriteSubTree); } myRegOne.Close(); } //打开项 private void Button_Click_1(object sender, RoutedEventArgs e) { RegistryKey lm = Registry.CurrentUser; //打开 RegistryKey myRegOne = lm.OpenSubKey("myRegOne"); //获取所有的项 string[] subKeyNames = myRegOne.GetSubKeyNames(); listSubKey.Items.Clear(); foreach (var item in subKeyNames) { listSubKey.Items.Add(item); } //获取所有的键值 string[] valueNames = myRegOne.GetValueNames(); listNameValue.Items.Clear(); foreach (var item in valueNames) { listNameValue.Items.Add(item + "---" + myRegOne.GetValue(item, "没有设置值")); } myRegOne.Close(); } //删除项 private void Button_Click_2(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; RegistryKey myRegOne = cu.OpenSubKey("myRegOne", true);//指定是否具有写权限 //删除,指定如果操作失败时是否抛出异常 myRegOne.DeleteSubKey("mySubKey_Three", false); myRegOne.Close(); MessageBox.Show("删除成功!"); } //判断项是否存在 private void Button_Click_3(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; //打开项 RegistryKey myRegOne = cu.OpenSubKey("myRegOne"); if (myRegOne != null) { MessageBox.Show("指定项'myRegOne'存在!"); } }
3.值操作:
//设置或新建值 private void Button_Click(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; //打开项,指定写权限 RegistryKey myRegOne = cu.OpenSubKey("myRegOne", true); if (myRegOne != null) { //创建项,如果存在则覆盖 myRegOne.SetValue("Version", "1.23"); myRegOne.SetValue("Enable", 1, RegistryValueKind.DWord); myRegOne.SetValue("Expend", "hello world!", RegistryValueKind.ExpandString); MessageBox.Show("设置值成功"); } cu.Close(); } //读取值 private void Button_Click_1(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; //打开项 RegistryKey myRegOne = cu.OpenSubKey("myRegOne", true); if (myRegOne != null) { string[] names = myRegOne.GetValueNames(); listOne.Items.Clear(); foreach (var item in names) { listOne.Items.Add(item + "----" + myRegOne.GetValue(item)); } } cu.Close(); } //删除值 private void Button_Click_2(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; //打开项,指定写权限 RegistryKey myRegOne = cu.OpenSubKey("myRegOne", true); if (myRegOne != null) { myRegOne.DeleteValue("Expend"); MessageBox.Show("删除成功!"); } cu.Close(); } //判断值是否存在 private void Button_Click_3(object sender, RoutedEventArgs e) { RegistryKey cu = Registry.CurrentUser; //打开项 RegistryKey myRegOne = cu.OpenSubKey("myRegOne"); if (myRegOne != null) { if (myRegOne.GetValue("Expend") != null) { MessageBox.Show("该值存在"); } } }