zoukankan      html  css  js  c++  java
  • 设置注册表权限

    using Microsoft.Win32;
    using System.Security.AccessControl;
    using System.Security.Principal;

    public static string Get_Reg_Val(string GroupName, string ValueName, string DefaultVal)

            {
                try
                {
                    string sValue = "";
                    try
                    {
                        sValue = (Registry.GetValue(strRegFullPath + GroupName,
                            ValueName, "")).ToString();
                        if (sValue == "")
                            throw new Exception();
                    }
                    catch(Exception)
                    {
                        RegistrySecurity userSecurity = new RegistrySecurity();
                        RegistryAccessRule userRule = new RegistryAccessRule("Everyone", 
                                                                             RegistryRights.FullControl, 
                                                                             AccessControlType.Allow);


                        userSecurity.AddAccessRule(userRule);


                        //if Registry key not set, create key and set default value
                        RegistryKey newKeyPass = Registry.LocalMachine.CreateSubKey(strRegLMPath + GroupName, 
                                                                                    RegistryKeyPermissionCheck.ReadWriteSubTree, 
                                                                                    userSecurity);
                        newKeyPass.SetValue(ValueName, DefaultVal);
                        sValue = DefaultVal;
                    }
                    return sValue;
                }
                catch (Exception ex) 
                {
                    return ex.Message;
                }
            }
  • 相关阅读:
    第4章 函数
    第3章 文件处理和函数
    第2章 数据类型
    第 8章 面向对象补充和网络编程
    第7章 面向对象
    第6章 常用模块
    第 5 章 函数的补充和模块加载
    git 使用命令提交文件
    jquery 读取本地json文件数据
    mpvue 封装api请求接口
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434906.html
Copyright © 2011-2022 走看看