37

How to force my C# Winforms program run as administrator on any computer ? and any kind of OS ?

I need code solution (any sample code will be excellent)

Thanks in advance

Mehrad
  • 4,093
  • 4
  • 43
  • 61
Gold
  • 60,526
  • 100
  • 215
  • 315
  • Possible duplicate of [How to force my .NET application to run as administrator?](http://stackoverflow.com/questions/2818179/how-to-force-my-net-application-to-run-as-administrator) – amens May 27 '16 at 07:51

6 Answers6

63

You can embed this manifest into your application.

<?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" />
    <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>  
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
  • 25
    for full steps including how to add a manifest file in VS http://philippsen.wordpress.com/tag/requestedexecutionlevel-manifest/ – Aaron Anodide Sep 30 '14 at 00:49
  • 1
    I tried this .. but while publishing the project, I get this error: `ClickOnce does not support the request execution level 'requireAdministrator'.` – Adil Malik Dec 24 '20 at 17:02
12

Here is the sample code to run your application as admin.

ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas";
try
{
    Process.Start(proc);
}
catch
{
    // The user refused the elevation.
    // Do nothing and return directly ...
    return;
}
Application.Exit();  // Quit itself

Set the ProcessStartInfo.Verb to “runas” will let it run as admin. Here is related FAQ

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/28f84724-af3e-4fa1-bd86-b0d1499eaefa#x_FAQAnswer91

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
  • 2
    I think OP is asking for how to force his application to always under with admin priv's not how to run any application from within a c# app as administrator – Rune FS Aug 30 '10 at 08:25
2

The obvious answer is to add a manifest file to the C# project and add the following line:

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

But, a rather unorthodox approach can also be taken. We know that registry access requires administrator privileges. So, if you have a function that contains a registry write access, the function will throw a System.Security.SecurityException if you don't run the program as an administrator. It is implied that you have to call this function at the beginning of the program. If this exception is thrown, you can inform the user to run the program as an administrator and close the program.

public void enforceAdminPrivilegesWorkaround()
{
    RegistryKey rk;
    string registryPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\";

    try
    {
        if(Environment.Is64BitOperatingSystem)
        {
            rk = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
        }
        else
        {
            rk = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
        }

        rk = rk.OpenSubKey(registryPath, true);
    }
    catch(System.Security.SecurityException ex)
    {
        MessageBox.Show("Please run as administrator");
        System.Environment.Exit(1);
    }
    catch(Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

Here, the true in line rk = rk.OpenSubKey(registryPath, true) tells the program that it needs write access to the registry.

Ishrak
  • 509
  • 1
  • 9
  • 17
1

It needs the Manifest file. Just place a manifest file and choose AsInvoker or AsAdministrator.

If you can access process you might use proc.Verb = "runas";

Check this : http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/db6647a3-85ca-4dc4-b661-fbbd36bd561f

abhishek
  • 2,975
  • 23
  • 38
0

I do not think you can force an application to run in Administrative mode. If you can then hackers can fully control your pc

  • 5
    You cannot *silently* run as admin. With the correct manifest, the app will *ask* for admin rights when started and fail if not granted. – Hans Kesting Sep 11 '20 at 12:16
-1

1-Create an Application Manifest:
In order to run your application with administrative privileges, you need to create an application manifest file. This XML file will inform the operating system that your application requires elevated privileges.

  • Right-click on your project in the Solution Explorer.

  • List item Add > New Item > Application Manifest File.

2- Edit the Application Manifest:
Open the newly created app.manifest file. You'll find a commented section labeled requestedExecutionLevel.
Uncomment this section and set the level attribute to "requireAdministrator":

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
  <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
      <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>
</security>

3-Build and Run.

saleem
  • 295
  • 2
  • 7
  • 15