如何检查应用程序是否以管理员身份运行 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43494776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:15:29  来源:igfitidea点击:

How to check if application is running as administrator VB.NET

vb.netadminvb.net-2010

提问by Ali Ahmad

I have made an application and some of its features only work with admin rights,

我制作了一个应用程序,它的一些功能仅适用于管理员权限,

How can I check if application is running with admin rights or not ?

如何检查应用程序是否以管理员权限运行?

  • And show a message box if application is not running with admin rights to run as administrator.
  • 如果应用程序没有以管理员权限运行,则显示一个消息框以管理员身份运行。

回答by ABEL MASILA

Imports System.Security.Principal

Dim identity = WindowsIdentity.GetCurrent()
Dim principal = new WindowsPrincipal(identity)
Dim isElevated as Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator) 
If isElevated Then
  MessageBox.Show("Is Admin")
End If

In VB.Net there is even a shortcut for this:

在 VB.Net 中,甚至还有一个快捷方式:

If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrat??or) Then ... 

回答by Mederic

Just change the app.manifest to force require administration:

只需更改 app.manifest 以强制要求管理:

Solution Explorer --> My Project --> View Windows Settings

解决方案资源管理器 --> 我的项目 --> 查看 Windows 设置

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

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

回答by Mousa Alfhaily

You can try and create a file on the system drive inside a try/catch block, and if it catches an access denied exception, then that means that the app is not running as administrator.

您可以尝试在系统驱动器上的 try/catch 块内创建一个文件,如果它捕获访问被拒绝的异常,则意味着该应用程序未以管理员身份运行。

Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            File.Create("c://test.txt")
            MessageBox.Show("Is Admin")
            File.Delete("c://test.txt")
        Catch ex As Exception
            If ex.ToString.Contains("denied") Then
                MessageBox.Show("Is Not Admin")
            End If
        End Try
    End Sub
End Class

回答by Glenn Slayden

A version for C# 6(or later), adapted from the Visual Basicsolution posted elsewhere on this page. In the interest of general-purpose use1, here I conceive a bool-valued static property:

(using System.Security.Principal;)

一个版本的C#6(或更高版本),改编自Visual Basic中解决此页面上的其他地方发布。在一般用途的利息1,在这里我想到一个bool-valued静态属性

using System.Security.Principal;

public static bool IsAdministrator =>
    new WindowsPrincipal(WindowsIdentity.GetCurrent())
            .IsInRole(WindowsBuiltInRole.Administrator);


Now having shown this, one must acknowledge that @Mederic's approach does indeed seem superior, since it's unclear precisely what an app might helpfully do after presumably detecting—and reporting—that such a (presumably) critical precondition has failed. Surely it's wiser—and safer—to delegate concerns of this nature to the OS.


现在已经证明了这一点,人们必须承认@Mederic 的方法确实看起来更胜一筹,因为目前尚不清楚应用程序在可能检测到并报告这样一个(可能的)关键先决条件失败后可能会做什么有帮助。当然,将这种性质的问题委托给操作系统更明智也更安全。




1That is, eliding the "MessageBox" desiderata articulated by the OP.


1也就是说,省略MessageBox了 OP 所阐明的“ ”需求。