zoukankan      html  css  js  c++  java
  • 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);
                }
            }
        }
    }

  • 相关阅读:
    【c++算法】移除性算法
    【c++容器】标准库与boost库中一些容器的介绍
    【Marva Collins' Way】第十章
    pc后时代的vs2012
    【行业关注】决策
    .net控件
    Silverlight 全屏显示
    ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR 异常
    Silverlight跨域,Silverlight在IIS中部署等问题解决之道
    演练:使用 Expression Blend 或代码创建 Silverlight 时钟
  • 原文地址:https://www.cnblogs.com/karrydong/p/2802509.html
Copyright © 2011-2022 走看看