2

出现此错误

''System.Security.SecurityException: Requested registry access is not allowed.
   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)''

当我尝试向注册表添加密钥时。我提到应用程序 requestedExecutionLevel 是“requireAdministrator”。启用 ClickOnce 安全设置并保留 requestedExecutionLevel ''asinvoker' 是一个更好的主意吗?

这是 VB.NET 代码的结构:

Try
[my code]
Catch sec As Security.SecurityException
         [another block of code]
Catch ex As Exception
         [another block of code]
End Try

使用“on error resume next”语句是一个更好的主意吗?请向我解释为什么会出现这个错误?

VB.NET、Visual Studio 2008(在 Vindows Vista Ultimate x86 和 Windows 7 Ultimate x64 上出现错误,我以管理员帐户登录)

4

1 回答 1

0

用户帐户控制 (UAC) 甚至需要管理员组帐户来提升应用程序。该提升必须由用户在应用程序开始时通过提交 UAC 对话框授予。

所以你需要编辑你的 app.manifest 使用

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

如果您不希望您的应用程序在每次启动时触发 UAC,您可以将需要提升到另一个可执行文件的操作外包,并像这样从未提升的进程中调用它

Dim elevatedProc As New Process
elevatedProc.FileName= "elevated.exe"
Try
elevatedProc.Start()
Catch ex As System.ComponentModel.Win32Exception
 MsgBox("Please commit UAC dialog")
End Try

如果用户未提交 UAC 对话框,则会引发 System.ComponentModel.Win32Exception。

或者,您可以在系统帐户下运行的服务中运行您的东西,这根本不会触发 UAC。

于 2012-12-20T23:59:39.343 回答