Link to home
Start Free TrialLog in
Avatar of Clif
ClifFlag for United States of America

asked on

Setup - Run As Administrator?

I'm working with VB.Net 2010 (Pro)

I've written a couple of applications for our engineers.  It seems like once a week or so I get an defect call (from different engineers) saying the app can't read from it's settings folder.  I have traced this down to the fact that the app has to be run "as administrator".  

One would think that an engineer would pay attention to the install and run directions, but that's neither here nor there.

Suffice it to say, I'm getting tired of repeating myself and telling the engineers to rtfm.

Is there any way I can setup the deployment so that my app will always "run as administrator"?

The setup project I've been using is "Visual Studio Installer" / "Setup Project", and I would like to retain that, if possible.

TIA
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clif

ASKER

This will solve my problem (on Windows 7)?

There is no solution when running under Win XP?
The solution for XP seems to be to create an extra program that launches the main program with a "runas" command.
Dim psi As New ProcessStartInfo
psi.Verb = "runas"
psi.UseShellExecute = True
psi.FileName = "notepad.exe"

Process.Start(psi)

Open in new window

Avatar of Clif

ASKER

Is it possible to put this in the startup routine of my app that will re-start my app in administrator mode, and of course shut down the first instance once the second has been started?
Maybe...  Try this.  Your project has to use Sub Main to launch:
    Public Sub Main()

        If IsRunningAsLocalAdmin() Then
            Application.Run(Form1)
        Else

            Dim psi As New ProcessStartInfo
            psi.Verb = "runas"
            psi.UseShellExecute = True
            psi.FileName = Application.ExecutablePath

            Process.Start(psi)
        End If

       
    End Sub

    'Function from here: http://blogs.msdn.com/b/jaredpar/archive/2007/08/01/detecting-if-you-are-an-admin.aspx
    Function IsRunningAsLocalAdmin() As Boolean
        Dim cur As WindowsIdentity = WindowsIdentity.GetCurrent()
        For Each role As IdentityReference In cur.Groups
            If role.IsValidTargetType(GetType(SecurityIdentifier)) Then
                Dim sid As SecurityIdentifier = DirectCast(role.Translate(GetType(SecurityIdentifier)), SecurityIdentifier)
                If sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) OrElse sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid) Then
                    Return True
                End If

            End If
        Next

        Return False
    End Function

Open in new window

Avatar of Clif

ASKER

If you can answer my last question, fine.  If not, that's ok.

Thanks.  :)
Avatar of Clif

ASKER

Thanks.  :)