编程语言
首页 > 编程语言> > C# 注册表基本操作

C# 注册表基本操作

作者:互联网

// 判断项是否存在

 private static bool IsRegeditItemExist(Microsoft.Win32.RegistryKey RegBoot, string ItemName)
 {
     if (ItemName.IndexOf("\\") <= -1)
     {
     string[] subkeyNames;
     subkeyNames = RegBoot.GetValueNames();
     foreach (string ikeyName in subkeyNames)   // 遍历整个数组
     {
     if (ikeyName == ItemName)  // 判断子项的名称
     {
     return true;
     }
     }
     return false;
     }
     else
     {
     string[] strkeyNames = ItemName.Split('\\');
     Microsoft.Win32.RegistryKey _newsubRegKey = RegBoot.OpenSubKey(strkeyNames[0]);
     string _newRegKeyName = "";
     int i;
     for (i = 1; i < strkeyNames.Length; i++)
     {
     _newRegKeyName = _newRegKeyName + strkeyNames[i];
     if (i != strkeyNames.Length - 1)
     {
     _newRegKeyName = _newRegKeyName + "\\";
     }
     }
     return IsRegeditItemExist(_newsubRegKey, _newRegKeyName);
     }
 }

// 判断键值是否存在

private static bool IsRegeditKeyExist(Microsoft.Win32.RegistryKey RegBoot, string RegKeyName)
{
    string[] subkeyNames;
    subkeyNames = RegBoot.GetValueNames();
    foreach (string keyName in subkeyNames)
    {

    if (keyName == RegKeyName)   // 判断键值的名称
    {
    return true;
    }
    }
    return false;
}

//读取注册表指定键值

public static string ReadRegedit(string RegKeyName)
{
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser;


    if (!IsRegeditItemExist(key.OpenSubKey("software\\ThirdParty\\"), "Register.INI"))
    {
    // 创建
    Microsoft.Win32.RegistryKey software = key.CreateSubKey("software\\ThirdParty");
    // 打开(true表示可以写入)
    software = key.OpenSubKey("software\\ThirdParty", true);
    Microsoft.Win32.RegistryKey ThirdPartyReg = software.CreateSubKey("Register.INI");
    // 记得关闭,两个都要关
    key.Close();
    ThirdPartyReg.Close();
    }
    if (!IsRegeditKeyExist(key.OpenSubKey("software\\ThirdParty\\Register.INI"), RegKeyName))
    {
    Microsoft.Win32.RegistryKey Register = key.OpenSubKey("SOFTWARE\\ThirdParty\\Register.INI", true);
    // 写入
    Register.SetValue(RegKeyName, "");
    // 关闭
    Register.Close();
    key.Close();
    }
    //判断软件是否注册
    Microsoft.Win32.RegistryKey retkey = key.OpenSubKey("SOFTWARE\\ThirdParty\\Register.INI", true);
    string str = retkey.GetValue(RegKeyName).ToString();
    retkey.Close();
    key.Close();
    return str;

}

//写入指定键值

public static bool SetRegedit(string RegKeyName, string Value)
{
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser;


    if (!IsRegeditItemExist(key.OpenSubKey("software\\ThirdParty\\"), "Register.INI"))
    {
    // 创建
    Microsoft.Win32.RegistryKey software = key.CreateSubKey("software\\ThirdParty");
    // 打开(true表示可以写入)
    software = key.OpenSubKey("software\\ThirdParty", true);
    Microsoft.Win32.RegistryKey ThirdPartyReg = software.CreateSubKey("Register.INI");

    // 记得关闭,两个都要关
    key.Close();
    ThirdPartyReg.Close();
    }
    if (!IsRegeditKeyExist(key.OpenSubKey("software\\ThirdParty\\Register.INI"), RegKeyName))
    {
    Microsoft.Win32.RegistryKey Register = key.OpenSubKey("SOFTWARE\\ThirdParty\\Register.INI", true);
    // 写入
    Register.SetValue(RegKeyName, "");
    // 关闭
    Register.Close();
    key.Close();
    }
    //判断软件是否注册
    Microsoft.Win32.RegistryKey retkey = key.OpenSubKey("SOFTWARE\\ThirdParty\\Register.INI", true);
    // 写入
    retkey.SetValue(RegKeyName, Value);
    retkey.Close();
    key.Close();
    return true;

}

//网络素材仅限收藏 方便学习

标签:ThirdParty,C#,Register,Win32,key,注册表,基本操作,Microsoft,software
来源: https://www.cnblogs.com/New-HackerHK/p/14858458.html