Invoking the Windows Security Dialog (in C# or VB.NET)

前端 未结 5 1209
你的背包
你的背包 2021-02-06 04:31

I would like to know how to invoke the Windows Security Dialog (press ctrl+alt+del on a windows workstation NOTE: I don\'t want the task manager!) programmatically.

5条回答
  •  星月不相逢
    2021-02-06 05:23

    This is a workaround i found on other forum. It works for me.

    Private Function IsRunAsAdministrator() As Boolean
    
        Dim wi = WindowsIdentity.GetCurrent()
        Dim wp = New WindowsPrincipal(wi)
    
        Return wp.IsInRole(WindowsBuiltInRole.Administrator)
    
    End Function
    

    'i use it on formload, you can have it whenever needed

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        If Not IsRunAsAdministrator() Then
            Dim processInfo = New ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)
            processInfo.UseShellExecute = True
            processInfo.Verb = "runas"
            Try
    
                Process.Start(processInfo)
    
            Catch ew1 As Exception
                MessageBox.Show("Sorry, this application must be run as Administrator.")
            End Try
            'Application.Current.Shutdown()
            Application.ExitThread()
        Else
       'your statement here
      End If
    End Sub
    

提交回复
热议问题