I'm writing a replacement to the windows event viewer, and as such, I require my program to start with admin privileges by default. Yes, I could run it without admin privileges and then have it elevate as required, but that could get very annoying very fast as the security log requires admin privileges to start with. How can I make the program assume admin privileges when I start it, and request access through UAC?

If possible, I'd like to also make this an optional function for those admins who are OK with the constant UAC nagging. Is there a simple way I can set an option in the program's interface to specify whether or not the program should default to admin access, or just use it as needed?

Recommended Answers

All 3 Replies

Hi,

You can use ProcessStartInfo class or WindowsIdentity (Windows Impersonation).

Maybe I'm misunderstanding the idea behind ProcessStartInfo, but from reading the MSDN page on it, it would seem that you can only use this when you are calling another process from within the C# program itself. What I want is for this program to run as an administrator when I run the main executable. I'm wondering if this is not a setting of some sort from within Visual Studio, instead of C# code itself.

To require administrative rights you need to embed and application manifest file. You can do this by "File -- New -- ... -- Application Manifest File". This is the default manifest:

<?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="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

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

            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

In visual studio 2008 the manifest will be automatically embedded by msbuild. In VS2005 you need to setup mt.exe as a post-build event in the IDE. Set the requestedExecutionLevel to requireAdministrator if you want to run as administrator when the process starts. This is compiled in to the binary so it always requires UAC, no exceptions.

For optional UAC you will have to use the "runas" verb in Process.Start() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.