程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#操作注冊表的方法

C#操作注冊表的方法

編輯:C#基礎知識
using Microsoft.Win32 ;  

1.讀取指定名稱的注冊表的值  

代碼如下:

private string GetRegistData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}

以上是讀取的注冊表中HKEY_LOCAL_MACHINE\SOFTWARE目錄下的XXX目錄中名稱為name的注冊表值; 

2.向注冊表中寫數據  

代碼如下:

private void WTRegedit(string name,string tovalue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("XXX");
aimdir.SetValue(name,tovalue);
}


以上是在注冊表中HKEY_LOCAL_MACHINE\SOFTWARE目錄下新建XXX目錄並在此目錄下創建名稱為name值為tovalue的注冊表項; 

3.刪除注冊表中指定的注冊表項  

代碼如下:

private void DeleteRegist(string name)
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
aimnames = aimdir.GetSubKeyNames();
foreach(string aimKey in aimnames)
{
if(aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}


以上是在注冊表中HKEY_LOCAL_MACHINE\SOFTWARE目錄下XXX目錄中刪除名稱為name注冊表項; 

4.判斷指定注冊表項是否存在  

代碼如下:

private bool IsRegeditExit(string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
subkeyNames = aimdir.GetSubKeyNames();
foreach(string keyName in subkeyNames)
{
if(keyName == name)
{
_exit = true;
return _exit;
}
}
return _exit;
}


以上是在注冊表中HKEY_LOCAL_MACHINE\SOFTWARE目錄下XXX目錄中判斷名稱為name注冊表項是否存在,這一方法在刪除注冊表時已經存在,在新建一注冊表項時也應有相應判斷; 

代碼如下:

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

namespace RegeditManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //創建並寫入
        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine;
            try
            {
                RegistryKey software = key.CreateSubKey("software\\LabManager");
                software = key.OpenSubKey("software\\LabManager", true);
                software.SetValue("Address", @"C:\Program Files\實驗室管理系統\dbcom.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        //讀取
        private void button2_Click(object sender, EventArgs e)
        {
            string info = string.Empty;
            RegistryKey key=Registry.LocalMachine;
            try
            {
                key = key.OpenSubKey("software\\LabManager");
                if (IsRegeditKeyExit("software\\LabManager", "Address"))
                {
                    info = key.GetValue("Address").ToString();
                    MessageBox.Show("注冊表裡的信息為:" + info);
                }
                else
                {
                    MessageBox.Show("鍵值Address不存在;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        //刪除
        private void button3_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine;
            try
            {
                key = key.OpenSubKey("software\\LabManager",true);
                if (IsRegeditKeyExit("software\\LabManager", "Address"))
                {
                    key.DeleteValue("Address");
                    MessageBox.Show("刪除成功");
                }
                else
                {
                    MessageBox.Show("鍵值Address不存在;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }

        /// <summary>
        /// 判斷鍵值是否存在
        /// </summary>
        /// <param name="RegistryStr">注冊表項</param>
        /// <param name="KeyStr">鍵值</param>
        /// <returns></returns>
        private bool IsRegeditKeyExit(string RegistryStr,string KeyStr)
        {
            string[] subkeyNames;

            RegistryKey hkml = Registry.LocalMachine;

            RegistryKey software = hkml.OpenSubKey(RegistryStr);

            subkeyNames = software.GetValueNames();

            foreach (string keyName in subkeyNames)
            {
                if (keyName == KeyStr)  //判斷鍵值的名稱
                {
                    hkml.Close();

                    return true;
                }
            }
            hkml.Close();

            return false;
        }

        /// <summary>
        /// 判斷注冊表項是否存在
        /// </summary>
        /// <param name="RegistryName">例如:SOFTWARE</param>
        /// <param name="ValueStr">例如:LabManager</param>
        /// <returns></returns>
        private bool IsRegeditItemExist(string RegistryName,string ValueStr)
        {
            string[] subkeyNames;

            RegistryKey hkml = Registry.LocalMachine;

            RegistryKey software = hkml.OpenSubKey(RegistryName);

            subkeyNames = software.GetSubKeyNames();

            //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中

            foreach (string keyName in subkeyNames)  //遍歷整個數組
            {
                if (keyName == ValueStr) //判斷子項的名稱
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;

        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved