打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【VS调试】C#读写Windows 7注册表时抛出“不允许所请求的注册表访问权”的解决办法

    在XP/2003下调试得好好的程序,到了windows7下,却抛出“不允许所请求的注册表访问权”的异常,该异常就在读写注册表时引发,例:

  1. public class Program  
  2.     {  
  3.         public static void SetAutoRun(string keyName, string filePath)  
  4.         {  
  5.             using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"software\microsoft\windows\currentversion\run"true))  
  6.             {  
  7.                 runKey.SetValue(keyName, filePath);  
  8.                 runKey.Close();  
  9.             }  
  10.         }  
  11.   
  12.         static void Main()  
  13.         {  
  14.             string keyName = "TestAutoRun";  
  15.             string fliePath = @"D:\CSharpProject\CSharp_Learn\MyDelegate.exe";  
  16.             SetAutoRun(keyName, fliePath);  
  17.         }  
  18.     }  
  19. }  

该程序如在windows 7下运行,需以管理员权限运行。

 

【方法一】

注册表

代码访问安全性策略必须向使用 Microsoft.Win32.Registry 类访问注册表的代码授予 RegistryPermission。这个权限类型可以用于限制对特定注册表项和子注册表项的注册表访问,还可以控制代码读取、写入或创建注册表项和已命名的值的能力。

约束注册表访问

要约束代码对特定注册表项的访问,可以使用带 SecurityAction.PermitOnly 的 RegistryPermissionAttribute。下面的属性确保代码仅可以读 HKEY_LOCAL_MACHINE\SOFTWARE 下面的 YourApp 注册表项(及子项)。

[RegistryPermissionAttribute(SecurityAction.PermitOnly,Read=@"HKEY_LOCAL_MACHINE\SOFTWARE\YourApp")]public static string GetConfigurationData( string key, string namedValue ){return (string)Registry.LocalMachine.OpenSubKey(key).GetValue(namedValue);}

请求 RegistryPermission

要记录代码的权限要求,并确保在代码访问安全性策略没有授予它充分的注册表访问权限时程序集无法加载,应当添加带 SecurityAction.RequestMinimum 的程序集级 RegistryPermissionAttribute,如下面的示例所示。

[assembly:RegistryPermissionAttribute(SecurityAction.RequestMinimum,Read=@"HKEY_LOCAL_MACHINE\SOFTWARE\YourApp")]

 

【方法二】

添加 应用程序清单文件,在其中加入

  1. <security>  
  2.       <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">  
  3.         <!-- UAC 清单选项  
  4.             如果希望更改 Windows 用户帐户控制级别,请用以下节点之一替换   
  5.             requestedExecutionLevel 节点。  
  6.   
  7.         <requestedExecutionLevel  level="asInvoker" uiAccess="false" />  
  8.         <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />  
  9.         <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />  
  10.   
  11.             如果您希望利用文件和注册表虚拟化提供  
  12.             向后兼容性,请删除 requestedExecutionLevel 节点。  
  13.         -->  
  14.         <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
  15.       </requestedPrivileges>  
  16.     </security>  

 

修改代码,如下:

  1. System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();  
  2. System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal( identity );  
  3. if(principal.IsInRole( System.Security.Principal.WindowsBuiltInRole.Administrator ))  
  4. {  
  5.     // 修改注册表  
  6. }  
  7. else  
  8. {  
  9.        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();  
  10.        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; // 获取当前可执行文件的路径及文件名   
  11.         //以下 Args 为启动本程序的对应的参数  
  12.         startInfo.Arguments = String.Join( " ", Args );  
  13.        startInfo.Verb = "runas";  
  14.        System.Diagnostics.Process.Start( startInfo );  
  15. }  


 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Windows) (用户帐户控制组策略和注册表项设置 | Microsoft Learn
调整Windows XP注册表 享受极速上网
原来Windows XP还可以这样重启
巧把Win10窗口背景色修改为养眼的淡绿色
win7注册表打不开 注册表编辑器打不开的解决方法
技巧|需要的网页内容被限制复制怎么办?教你一招解锁!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服