Solved

Setup - Run As Administrator?

Posted on 2012-04-05
7
359 Views
Last Modified: 2012-04-05
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
0
Comment
Question by:Clif
  • 4
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37812813
You can make your program request elevation by adding a manifest.  (This won't work if the system is running XP)

Go to project properties.  Select application tab.  Click the button for "View Windows Settings"

Find the line in the manifest that says
<requestedExecutionLevel  level="asInvoker" uiAccess="false" />
and change it to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
0
 
LVL 10

Author Comment

by:Clif
ID: 37812965
This will solve my problem (on Windows 7)?

There is no solution when running under Win XP?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37812983
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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Author Comment

by:Clif
ID: 37813192
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?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37813347
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

0
 
LVL 10

Author Closing Comment

by:Clif
ID: 37813349
If you can answer my last question, fine.  If not, that's ok.

Thanks.  :)
0
 
LVL 10

Author Comment

by:Clif
ID: 37813661
Thanks.  :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now