首页 > 编程语言 >[C#]注册表操作

[C#]注册表操作

时间:2023-02-15 14:23:08浏览次数:38  
标签:RegistryKey theRegistrySubKey C# 注册表 Close Registry theRegistryKey 操作 CurrentUse

创建

RegistryKey theRegistryKey = Registry.CurrentUser;
theRegistryKey.CreateSubKey("MyApp");
theRegistryKey.SetValue("MyValue0", "MyValueData0");
theRegistryKey.SetValue("MyValue1", "MyValueData1");

RegistryKey theRegistrySubKey = Registry.CurrentUser.CreateSubKey("MyApp\\DEMO");
theRegistrySubKey.SetValue("MyValue2", "MyValueData2");
theRegistrySubKey.SetValue("MyValue3", "MyValueData3");
theRegistryKey.Close();
theRegistrySubKey.Close();

打开/读取

RegistryKey theRegistryKey = Registry.CurrentUser;
//RegistryKey theRegistrySubKey = Registry.CurrentUser.CreateSubKey("MyApp\\DEMO");
RegistryKey theRegistrySubKey = Registry.CurrentUser.OpenSubKey("MyApp\\DEMO");
Console.WriteLine(theRegistryKey.GetValue("MyValue0"));
Console.WriteLine(theRegistryKey.GetValue("MyValue1"));
Console.WriteLine(theRegistrySubKey.GetValue("MyValue2"));
Console.WriteLine(theRegistrySubKey.GetValue("MyValue3"));
theRegistryKey.Close();
theRegistrySubKey.Close();

删除

RegistryKey theRegistryKey = Registry.CurrentUser;
RegistryKey theRegistrySubKey = Registry.CurrentUser.OpenSubKey("MyApp",true);
theRegistryKey.DeleteValue("MyValue0");
theRegistrySubKey.DeleteSubKeyTree("DEMO");
theRegistryKey.DeleteSubKey("MyApp");
theRegistryKey.Close();
theRegistrySubKey.Close();

判断项是否存在

private bool RegExist()
{
    string[] strSubNames;
    RegistryKey theRegistryKey = Registry.CurrentUser;
    RegistryKey theRegistrySubKey = theRegistryKey.OpenSubKey("MyApp\\DEMO");
    strSubNames = theRegistrySubKey.GetSubKeyNames();
    foreach (string strSubName in strSubNames)
    {
        if (strSubName == "MyValue0")
        {
            theRegistryKey.Close();
            theRegistrySubKey.Close();
            return true;
        }
    }
    theRegistryKey.Close();
    theRegistrySubKey.Close();
    return false;
}

判断值是否存在

private bool RegKeyExist()
{
    string[] strKeyNames;
    RegistryKey theRegistryKey = Registry.CurrentUser;
    RegistryKey theRegistrySubKey = theRegistryKey.OpenSubKey("MyApp\\DEMO");
    strKeyNames = theRegistrySubKey.GetValueNames();
    foreach (string strKeyName in strKeyNames)
    {
        if (strKeyName == "MyValue2")
        {
            theRegistryKey.Close();
            theRegistrySubKey.Close();
            return true;
        }
    }
    theRegistryKey.Close();
    theRegistrySubKey.Close();
    return false;
}

 

 

标签:RegistryKey,theRegistrySubKey,C#,注册表,Close,Registry,theRegistryKey,操作,CurrentUse
From: https://www.cnblogs.com/sairenjiharuna/p/17122649.html

相关文章

  • c++中nan,inf
    nan:notanumber非数字注意事项:对负数开方sqrt(-1.0)、对负数求对数(log(-1.0))、0.0/0.0、0.0*inf、inf/inf、inf-inf这些操作都会得到nan。(0/0会产生操作异常;0.0/0.0......
  • 装备制造业数字化转型CRM系统解决方案(信息图)
    一、制造企业面临的机遇与挑战2021年12月28日,工业和信息化部等八部门联合对外发布《“十四五”智能制造发展规划》,明确提到“推进智能制造,要立足制造本质,紧扣智能特征,以工......
  • 将博客搬至CSDN
    尊敬的CSDN工作人员:您好!&nsbp;&nsbp;很开心看到CSDN可以搬家,我这边蠢蠢欲动地试试看吧,恭喜发财,谢谢谢谢!&nsbp;&nsbp;此致&nsbp;&nsbp;敬礼!......
  • ignite系列之4--jdbc端口xml配置
    jdbc端口xml配置<!--jdbc端口范围配置--><propertyname="clientConnectorConfiguration"><beanclass="org.apache.ignite.configuration.Clie......
  • openoffice 文件转化为pdf
    /**转换组件属性设置*/functionOpenOfficeMakePropertyValue($name,$value,$osm){$oStruct=$osm->Bridge_GetStruct("com.sun.star.beans.PropertyValu......
  • eclipse中robot项目界面配置
    1、调整为robot视图 2、运行时报编码错误,调整编码配置 3、查看打印结果,点击后添加输出栏 ......
  • 直播系统搭建,docker Elasticsearch 7.16.1 设置密码
    直播系统搭建,dockerElasticsearch7.16.1设置密码1、启动容器 dockerrun-d-p9200:9200-p9300:9300--hostnamees--networkseata_default-e"discovery.typ......
  • [第三章]ABAQUS CM插件中文手册
    ABAQUSCompositeModelerUserManual(zh-CN)©DassaultSystèmes,2018注:源文档的交叉引用链接,本文无效有些语句英文表达更易理解,则保留原文3.Activation3......
  • Install Chia Blockchain on Ubuntu
    InstallChiaBlockchainonUbuntuPostedon May4,2021ChiaisanewkindaofCryptoCurrencythatinsteadofusingPoW(ProofofWork)itusesProofofSpa......
  • .Net6 WebApi中集成FluentValidation.AspNetCore的用法
    一、首先在nuget管理器中添加FluentValidation.AspNetCore包 二、添加验证类并继承AbstractValidator<T>,T为原始参数类,在验证类的构造函数中添加验证内容  三、......