zoukankan      html  css  js  c++  java
  • How to handle registry via C#

    How to handle registry key to achiecve our purpose via C# is very important especially for our testing. We often need to alter some registry key to change windows settings or read some window settings. Inorder handle registry we need using namespace Microsoft.Win32. Two main class provided are Registry and RegistryKey, Registry is main responsible for get static objects, and RegistryKey is provided methods to create and read registry. The main static methods in Registry:

    • ClassRoot : Provide access the root "HKEY_CLASSES_ROOT
    • CurrentConfig: Provide access the roor "HKEY_CURRENT_CONFIG"
    • CurrentUser: Provide access the root"HKEY_CURRENT_USER"
    • LocalMachine: Provide accee the root "HKEY_LOCAL_MACHINE"
    • USers: Provide access the root "HKEY_USER"
    • PerformanceData: Provide access the root "HKEY_PERFORMANCE_DATA"

    RegistryKey class is more important to handle registry, the main operations we used are use this class.

    Property: Name, SubKeyCount and ValueCount.

    Method: OpenSubKey, CreateSubKey, DeleteSubKey, Close, GetSubKeyName, GetValue, SetValue and DeleteValue. 

    Static Methods Example: 

    Open Registry(If need edit registry, set parameter set value as true)

      Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftSomeKey"); Only read

      Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftSomeKey",  true); Read and Write 

      Registry.CurrentUSer.DeleteSubKey(@"SoftwareMicrosoftSomeKey");


    Object Methods Example:

    Get Subkey and Edit

      RegistryKey rk = RegistryKey.CurrentUser.OpenSubKey(@"SoftWareMicrosoft", true);

      rk.SetValue("Name", "Eric");

      rk.SetValue("Version", "1.0.0");

      string name = rk.GetValue("Name"); 

      rk.DeleteValue("Version");  //Delete registryKey "Version"

     Note: 

    1.If you need to handle registry both in x86 and amd64 OS, you need attention to get above methods. Becasue different type of OS ,the registry path is different. You need reposition them like below under amd64 machine:
      Registry localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) 

    2. All operations about registry, we should got admin privilige first in case of some exception or errors. And we should more careful when we handle registry especially delete some keys.

  • 相关阅读:
    存储过程语法
    ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.
    ORACLE 存储过程异常捕获并抛出
    Sqlldr导入txt文件内容到数据库中
    ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项
    oracle将一个表中字段的值赋值到另一个表中字段(批量)
    (三)Solr——Solr的基本使用
    (二)Solr——Solr界面介绍
    jrebel 7免费激活(非破解) 和 IntelliJ Idea 2017 免费激活方法
    (一)Solr——简介和安装配置
  • 原文地址:https://www.cnblogs.com/Blackeye286/p/3580409.html
Copyright © 2011-2022 走看看