zoukankan      html  css  js  c++  java
  • 注册表操作类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Win32;//操作注册表的命名空间

    namespace WinXPReg
    {
     

      public class RegCtrl
        {
            
    private static RegistryKey rootkey;
            //
    构造根键为RootKey的注册表操作类,缺省打开Current_User主键 
            public RegCtrl(string RootKey)
            {
                
    switch (RootKey.ToUpper())
                {
                    
    case "CLASSES_ROOT":
                        rootkey 
    = Registry.ClassesRoot;
                        
    break;
                    
    case "CURRENT_USER":
                        rootkey 
    = Registry.CurrentUser;
                        
    break;
                    
    case "LOCAL_MACHINE":
                        rootkey 
    = Registry.LocalMachine;
                        
    break;
                    
    case "USERS":
                        rootkey 
    = Registry.Users;
                        
    break;
                    
    case "CURRENT_CONFIG":
                        rootkey 
    = Registry.CurrentConfig;
                        
    break;
                    
    case "DYN_DATA":
                        rootkey 
    = Registry.DynData;
                        
    break;
                    
    case "PERFORMANCE_DATA":
                        rootkey 
    = Registry.PerformanceData;
                        
    break;
                    
    default:
                        rootkey 
    = Registry.CurrentUser;
                        
    break;
                }
            }

            
    // 读取路径为keypath,键名为keyname的注册表键值,缺省返回def 

            public string GetRegVal(string keypath, string keyname, string def)
            {
                
    try
                {
                    RegistryKey key 
    = rootkey.OpenSubKey(keypath);
                    
    return key.GetValue(keyname, (object)def).ToString();
                }
                
    catch
                {
                    
    return def;
                }
            }

            
    /// /// 设置路径为keypath,键名为keyname的注册表键值为keyval /// /// /// /// 
            public bool SetRegVal(string keypath, string keyname, string keyval)
            {
                
    try
                {
                    RegistryKey key 
    = rootkey.OpenSubKey(keypath, true);
                    key.SetValue(keyname, (
    object)keyval);
                    
    return true;
                }
                
    catch
                {
                    
    return false;
                }
            }
            
    //创建路径为keypath的键 
            public RegistryKey CreateRegKey(string keypath)
            {
                
    try
                {
                    
    return rootkey.CreateSubKey(keypath);
                }
                
    catch
                {
                    
    return null;
                }
            }
            
    //删除路径为keypath的子项
            public bool DelRegSubKey(string keypath)
            {
                
    try {
                    rootkey.DeleteSubKey(keypath); 
    return true
                }
                
    catch 
                { 
                    
    return false;
                }
            }

            
    // 删除路径为keypath的子项及其附属子项 
            public bool DelRegSubKeyTree(string keypath)
            {
                
    try
                {
                    rootkey.DeleteSubKeyTree(keypath);
                    
    return true;
                }
                
    catch
                {
                    
    return false;
                }
            }
            
    //删除路径为keypath下键名为keyname的键值
            public bool DelRegKeyVal(string keypath, string keyname)
            {
                
    try
                {
                    RegistryKey key 
    = rootkey.OpenSubKey(keypath, true);
                    key.DeleteValue(keyname);
                    
    return true;
                }
                
    catch
                {
                    
    return false;
                }
            }
        }

    }

  • 相关阅读:
    [NC13331]城市网络
    Codeforces Round #638 (Div. 2)
    牛客练习赛62
    “科大讯飞杯”第18届上海大学程序设计联赛春季赛暨高校网络友谊赛
    Codeforces Round #635 (Div. 2)
    Codeforces Round #631 (Div. 2)
    牛客每日一题
    Codeforces Round #627 (Div. 3)
    MySQL查看建表语句
    Oracle的number数据类型
  • 原文地址:https://www.cnblogs.com/top5/p/1718933.html
Copyright © 2011-2022 走看看