首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么''Try ... Catch .. End Try‘不能捕获所有异常? VB.NET

为什么''Try ... Catch .. End Try‘不能捕获所有异常? VB.NET
EN

Stack Overflow用户
提问于 2012-12-19 20:42:25
回答 1查看 1.1K关注 0票数 2

出现此错误

代码语言:javascript
运行
复制
''System.Security.SecurityException: Requested registry access is not allowed.
   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)''

当我尝试将注册表项添加到注册表时。我提到的应用程序requestedExecutionLevel是"requireAdministrator“。这是一个更好的想法,以启用ClickOnce安全设置,并留下requestedExecutionLevel‘’为asinvoker“?

这是VB.NET代码的结构:

代码语言:javascript
运行
复制
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 Studio2008( Vindows Vista旗舰版x86和Windows7旗舰版x64出现错误,我是以管理员帐户登录的)

EN

回答 1

Stack Overflow用户

发布于 2012-12-21 07:59:39

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

因此,您需要使用以下命令编辑app.manifest

代码语言:javascript
运行
复制
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

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

代码语言:javascript
运行
复制
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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13952729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档