89

I need my software to be able to run as administrator on Windows Vista (if someone runs it without administrative permissions, it will crash).

When launching other software, I've seen a prompt by the system like "this software will run as administrator. do you want to continue?" when the app was trying to acquire administrative privileges.

How do I request administrative privileges when running an c# app on Windows Vista?

Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
  • 6
    Please don't forget that doing this only hides the underlying problem, it doesn't fix it. Even if your program really does need admin permissions, it shouldn't crash if it doesn't get them. The most likely cause is that you are failing to check for an error condition following a system call. – Harry Johnston Oct 05 '11 at 20:53

4 Answers4

155

Add the following to your manifest file:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

You can also use highestAvailable for the level.

Look here about embedding manifest files:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

PS: If you don't have a manifest file, you can easily add a new one:

In Visual Studio, right click project -> Add Item -> Choose Application Manifest File ( under General for Visual C# items)

The added file will already have the above part, just change the level to requireAdministrator from asInvoker

Peter
  • 4,752
  • 2
  • 20
  • 32
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 5
    Worth mentioning is that if you add an Application Manifest File in VS this way, you get a template there with many other options (like, saying that your app only works on Windows 10 and upwards.) Tested on VS2017. – Per Lundberg Oct 25 '17 at 05:57
  • @PerLundberg - Not worth mentioning, because the original Ask-er didn't ask about this -- He asked about Administrator Permissions – Momoro Dec 06 '19 at 19:33
  • 6
    @Momoro Questions on SO are not only for the OP, it's for the greater benefit of the community. Other people might find this question when looking for other solutions, not only _exactly_ what the OP asked about. And in general, it doesn't hurt to teach people about "you can do these things with application manifests". – Per Lundberg Dec 07 '19 at 20:32
  • I wasn't implying anything rude(Sorry if I came across that way) I was just reading up on old answers / questions, and got kind of confused :D – Momoro Dec 09 '19 at 23:50
  • Newer version of the link: https://learn.microsoft.com/en-us/previous-versions/bb756929(v=msdn.10)#how-to-create-an-embedded-manifest-with-microsoft-visual-studio – Deantwo Aug 08 '23 at 10:56
19

Put this XML in a file called yourexename.exe.manifest:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
   <security>
     <requestedPrivileges>
        <requestedExecutionLevel level="highestAvailable" />
     </requestedPrivileges>
   </security>
</trustInfo>
</assembly>
Bill Tarbell
  • 4,933
  • 2
  • 32
  • 52
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
1

You need to use the requestedExecutionLevel token in a manifest:

http://www.developerfusion.com/code/7987/making-a-net-app-run-on-vista-with-administrator-priviledges/

Polynomial
  • 27,674
  • 12
  • 80
  • 107
-2

For .Net (Visual Studio 2013), include a manifest file that request administrator elevation and using the compiler's /win32manifest flag, compose and provide a manifest file that requests this elevation. However, the following describe doing so within Visual Studio for a project name, App.Exe:

  1. Create a file with the following content (for convenience you may add the file to the Visual Studio project as a development resource by ensuring that it's Build Action is None and Copy to Output... is Do not copy. By convention manifest files are named after their output target, in this case App.Exe.manifest. If you require uiAccess (User Interface), the assembly must be strongly named.

    <?xml version="1.0" encoding="utf-8" ?>
    <asmv1:assembly manifestVersion="1.0" 
        xmlns="urn:schemas-microsoft-com:asm.v1"
        xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
        xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <assemblyIdentity version="1.0.0.0" name="App" />
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>
    </asmv1:assembly>
    
  2. Edit the project dialogue's build panel Other flags: entry field to add the win32manifest flag and have Visual Studio invoke the compiler accordingly. For example, in this case,

    /win32manifest:App.Exe.manifest.

Note the following entry:

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
George
  • 2,451
  • 27
  • 37