3

I am working on a small application in VB.NET. The program needs administrator privilege for doing some tasks. Is there a way to ask for administrator privileges during the execution if the program?

What is the general way of changing the user account under which the application is running?

Marcio Aguiar
  • 14,231
  • 6
  • 39
  • 42
Niyaz
  • 53,943
  • 55
  • 151
  • 182
  • 2
    Appears that you have posted the same question twice: http://stackoverflow.com/questions/90674/how-does-a-program-ask-for-administrator-privileges – Serhat Ozgel Sep 18 '08 at 07:26

4 Answers4

7

You can specify this in your application's manifest file.

Check out this link and this link and this link too.

J D OConal
  • 624
  • 4
  • 14
3

There are a number of methods depending on your needs. Some details are given in the application developer requirements for UAC.

  1. Include a UAC manifest that causes your program to require administrator privileges at startup.
  2. Use one of the suggested methods for invoking an elevation to run out of process. One of the nicest is to use the COM elevation moniker and CoCreateInstanceAsAdmin to call methods on a COM object running as an administrator. This is possibly tricky to get working in VB.Net. I got it working ok in C++ though
  3. Another ok method is to isolate the parts of your code that need admin privileges into an application that uses a UAC manifest to require admin privileges. Your main app does not need to run as an admin in that case. When you require admin privilegese, you would invoke the external application.
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • I used the Method 3, but it only works on Win7, it does not work on Vista, instead, giving me "System.ComponentModel.Win32Exception (0x80004005) the requested operation requires elevation" – Peter Lee Mar 27 '11 at 07:10
  • I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin - Reference: http://stackoverflow.com/a/8690661 – Kiquenet Aug 28 '14 at 09:27
  • @Kiquenet yes you are probably right, you would normally communicate using COM in that case by method 2 if you needed that kind of communication, but there are lots of other ways that could also be made to serve – 1800 INFORMATION Aug 28 '14 at 21:45
1
 Try
                    Dim procInfo As New ProcessStartInfo()
                    procInfo.UseShellExecute = True
                    procInfo.FileName = 'Filename here
                    procInfo.WorkingDirectory = ""
                    procInfo.Verb = "runas"
                    Process.Start(procInfo)
                Catch ex As Exception
                    MsgBox(ex.Message.ToString(), vbCritical)
                End Try
            End If
Curtis
  • 101,612
  • 66
  • 270
  • 352
AnOnYmOuS
  • 11
  • 1
0

The most easy way to do this is to click on the Project tab -> Add Windows Form -> .XML file -> name it (program name).manifest -> paste this code in this link into it ( thanks JDOConal ) -> then right click on your project name in the solution explorer box off to the right and hit properties -> on the first tab select manifest and then the .manifest file you created -> build = done!

llk
  • 2,501
  • 7
  • 36
  • 43