• C# 实现注册表的读取


    直接上方法,至于怎么调用,偶想大家应该会的

    using Microsoft.Win32;
    using System;
    using System.Windows.Forms;

    namespace Lambda
    {
        public class ModifyRegistry
        {
            private bool showError = false;
            private string subKey = "SOFTWARE\\、、、、、、、";//select where you want
            private Microsoft.Win32.RegistryKey baseRegistryKey = Microsoft.Win32.Registry.CurrentUser;
            public bool ShowError
            {
                get
                {
                    return this.showError;
                }
                set
                {
                    this.showError = value;
                }
            }
            public string SubKey
            {
                get
                {
                    return this.subKey;
                }
                set
                {
                    this.subKey = value;
                }
            }
            public Microsoft.Win32.RegistryKey BaseRegistryKey
            {
                get
                {
                    return this.baseRegistryKey;
                }
                set
                {
                    this.baseRegistryKey = value;
                }
            }
            public string Read(string KeyName)
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                string result;
                if (registryKey2 == null)
                {
                    result = null;
                }
                else
                {
                    try
                    {
                        result = (string)registryKey2.GetValue(KeyName.ToUpper());
                    }
                    catch (System.Exception e)
                    {
                        this.ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
                        result = null;
                    }
                }
                return result;
            }
            public bool Write(string KeyName, object Value)
            {
                bool result;
                try
                {
                    Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                    Microsoft.Win32.RegistryKey registryKey2 = registryKey.CreateSubKey(this.subKey);
                    registryKey2.SetValue(KeyName.ToUpper(), Value);
                    result = true;
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
                    result = false;
                }
                return result;
            }
            public bool DeleteKey(string KeyName)
            {
                bool result;
                try
                {
                    Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                    Microsoft.Win32.RegistryKey registryKey2 = registryKey.CreateSubKey(this.subKey);
                    if (registryKey2 == null)
                    {
                        result = true;
                    }
                    else
                    {
                        registryKey2.DeleteValue(KeyName);
                        result = true;
                    }
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Deleting SubKey " + this.subKey);
                    result = false;
                }
                return result;
            }
            public bool DeleteSubKeyTree()
            {
                bool result;
                try
                {
                    Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                    Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                    if (registryKey2 != null)
                    {
                        registryKey.DeleteSubKeyTree(this.subKey);
                    }
                    result = true;
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Deleting SubKey " + this.subKey);
                    result = false;
                }
                return result;
            }
            public int SubKeyCount()
            {
                int result;
                try
                {
                    Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                    Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                    if (registryKey2 != null)
                    {
                        result = registryKey2.SubKeyCount;
                    }
                    else
                    {
                        result = 0;
                    }
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Retriving subkeys of " + this.subKey);
                    result = 0;
                }
                return result;
            }
            public int ValueCount()
            {
                int result;
                try
                {
                    Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                    Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                    if (registryKey2 != null)
                    {
                        result = registryKey2.ValueCount;
                    }
                    else
                    {
                        result = 0;
                    }
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Retriving keys of " + this.subKey);
                    result = 0;
                }
                return result;
            }
            private void ShowErrorMessage(System.Exception e, string Title)
            {
                if (this.showError)
                {
                    MessageBox.Show(e.Message, Title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }
    }

  • 相关阅读:
    yaf将错误输出打印在页面上
    yaf设置命名空间
    yaf学习资料
    在 Github 上找「好东西」的方法
    在linux命令行下执行php 程序
    linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计
    php数组函数分析--array_column
    php 去掉字符串的最后一个字符
    设置arc 的默认编辑器
    需要学习的技术
  • 原文地址:https://www.cnblogs.com/karrydong/p/2802509.html
Copyright © 2020-2023  润新知