在Win7下编程时可能有这样的情况,代码没有问题,但是执行时却会出现异常,如:System.Security.SecurityException: 不允许所请求的注册表访问权。只有以管理员身份运行时才能够正常运行。
这是Win7下UCA在起作用了。那好在VS的安装目录下有这么一个文件,它叫app.manifest.
我们可以看一下security下的内容。
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 清单选项
如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换
requestedExecutionLevel 节点。
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
如果要利用文件和注册表虚拟化实现向后
兼容性,则删除 requestedExecutionLevel 节点。
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
就是说我们有三种用户控制级别,第三种不知道是做什么的,但是默认是第一种,改成第二种就可以解决以上问题了,相信就是管理员权限了。
第三种可能会更高,是不是在做驱动程序时会用到呢(猜想的)