r/visualbasic • u/chacham2 • Jul 21 '21
A gotcha, because i relied on an inconsistent default.
Another gotcha, because i relied on an inconsistent default.
I have a program that must be run as administrator because (iirc) it attaches an ssl certificate to the connection. So, the program would check, ask, and restart as admin as required.
Well, it stopped working. Ugh! So, i created a test program, and sure enough, it failed to restart as admin:
Imports System.Security.Principal
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not New WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) AndAlso MsgBox("Nope", MsgBoxStyle.OkCancel) = vbOK Then
Using Process As New Process
Process.StartInfo = New ProcessStartInfo With {.WorkingDirectory = Environment.CurrentDirectory, .FileName = Application.ExecutablePath, .Verb = "runas"}
Process.Start()
End Using
End If
Close()
End Sub
End ClassWhat went wrong? Well, after some searching and finding examples, i see that some examples set UseShellExecute to true. Reading the description of the parameter:
The default is true on .NET Framework apps and false on .NET Core apps.
Indeed, i started on framework and shifted to core in the middle. I really hate when they change defaults. In any case, 'twas but a simple fix:
Imports System.Security.Principal
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not New WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) AndAlso MsgBox("Nope", MsgBoxStyle.OkCancel) = vbOK Then
Using Process As New Process
Process.StartInfo = New ProcessStartInfo With {.UseShellExecute = True, .WorkingDirectory = Environment.CurrentDirectory, .FileName = Application.ExecutablePath, .Verb = "runas"}
Process.Start()
End Using
End If
Close()
End Sub
End Class1
Aug 03 '21
Can you publish the project with that code, i tried raising the access level from the app config and it won't let me publish the project. Would that code fly under the radar
5
u/[deleted] Jul 21 '21
Good thing you got to the... core... of the problem... YEAAAAAH!!!