c sharp operates on methods of the registry

  • 2020-05-05 11:47:03
  • OfStack

using   Microsoft.Win32   ;    

1. Reads the value    
of the registry with the specified name

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; 
} 

The above is the registry value of name in XXX directory under HKEY_LOCAL_MACHINE\SOFTWARE directory in HKEY_LOCAL_MACHINE\SOFTWARE directory.  

2. Write data    
to the registry

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); 
}


Above is the registry HKEY_LOCAL_MACHINE\SOFTWARE directory in the HKEY_LOCAL_MACHINE\SOFTWARE directory in the new XXX directory and in this directory to create the registry key name name value tovalue;  

3. Delete the registry key    
specified in the registry

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); 
} 
} 


Above is in the registry HKEY_LOCAL_MACHINE\SOFTWARE directory XXX directory to delete the name of name registry key;  

4. Determines whether the specified registry key exists    


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; 
}


Above is in the registry HKEY_LOCAL_MACHINE\SOFTWARE directory under the XXX directory to determine the name of the name registry key exists, this method has existed when the registry is deleted, in the new registry key should also be judged accordingly;  


using System.Windows.Forms;
using Microsoft.Win32;
namespace RegeditManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // Create and write 
        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\ Laboratory management system \dbcom.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        // read 
        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(" The information in the registry is :" + info);
                }
                else
                {
                    MessageBox.Show(" The key value Address There is no ;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        // delete 
        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(" Delete the success ");
                }
                else
                {
                    MessageBox.Show(" The key value Address There is no ;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        /// <summary>
        ///  Determines whether the key value exists 
        /// </summary>
        /// <param name="RegistryStr"> Registry entries </param>
        /// <param name="KeyStr"> The key value </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)  // Determines the name of the key value 
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
        /// <summary>
        ///  Determines whether a registry key exists 
        /// </summary>
        /// <param name="RegistryName"> For example, :SOFTWARE</param>
        /// <param name="ValueStr"> For example, :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();
            // Gets a sequence of the names of all the children under that item and passes them to a predetermined array 
            foreach (string keyName in subkeyNames)  // Iterate through the entire array 
            {
                if (keyName == ValueStr) // Determines the name of the child 
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
    }
}


Related articles: