Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace msdn
{
    class Program
    {
        static void Main(string[] args)

        {

            const string NO_DRIVE = @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
            try
            {
                using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(NO_DRIVE))
                {
                    // Your normal code goes here.
                    registryKey.SetValue("NoDrives",
                    Convert.ToInt32("8", 16), RegistryValueKind.DWord);
                    registryKey.Close();

                    Console.WriteLine("Successfully Locked");
                };
            }
            catch (UnauthorizedAccessException E)
            {
                Console.WriteLine("Access not allowed.");
            }

            Console.ReadLine();
        }
    }
}
Posted
Updated 9-Aug-14 23:42pm

1 solution

Hello,

You can't make your application run automatically with administrator privileges. But you can easily prompt for elevation requesting the user to allow the application to run with administrator rights.

Here is a way to do it:

First use Win32 to check if the current user has admin rigths:

C#
[DllImport("shell32.dll")]
internal static extern bool IsUserAnAdmin();


Then write a method to elevate your process:
C#
private static bool Elevate(string processPath)
{
    bool elevated = false;
    if (!IsUserAnAdmin())
    {
        elevated = true;
        ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
        procInfo.UseShellExecute = true;
        procInfo.Verb = "runas";
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process.Start(procInfo);
    }
    return elevated;
}


Finally put it all together:
C#
[DllImport("shell32.dll")]
internal static extern bool IsUserAnAdmin();

public static void Main(string[] args)
{
    if (!Elevate(System.Reflection.Assembly.GetExecutingAssembly().Location))
    {
        const string NO_DRIVE = @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
        try
        {
            using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(NO_DRIVE))
            {
                // Your normal code goes here.
                registryKey.SetValue("NoDrives",
                Convert.ToInt32("8", 16), RegistryValueKind.DWord);
                registryKey.Close();

                Console.WriteLine("Successfully Locked");
            };
        }
        catch (UnauthorizedAccessException E)
        {
            Console.WriteLine("Access not allowed.");
        }

        Console.ReadLine();
    }
}

private static bool Elevate(string processPath)
{
    bool elevated = false;
    if (!IsUserAnAdmin())
    {
        elevated = true;
        ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
        procInfo.UseShellExecute = true;
        procInfo.Verb = "runas";
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process.Start(procInfo);
    }
    return elevated;
}


What happens is that first time the code is executes, you call a win32 api that tells you if the user has admin rights or not, if not, you run startup your process using the "runas" verb that triggers elevation request and your process is not running as administrator.

There are other way to achieve the same using application manifest etc... Google is your friend...


Valery.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900