Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setup - Run As Administrator?

Posted on 2012-04-05
7
Medium Priority
?
396 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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