简体   繁体   中英

Running CMD as administrator with an argument from C#

I want to run cmd.exe as administrator with arguments from C# in order to prevent a UAC popup. This is necessary in order to use it as an automated installation process. The command I am passing in is simply a path to installation file (.exe) with /q for quiet installation.

When I run this code, there is a CMD popup, but it runs as if it didn't execute anything.

public static string ExecuteCommandAsAdmin(string command)
{

    ProcessStartInfo procStartInfo = new ProcessStartInfo()
    {
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        CreateNoWindow = true,
        FileName = "runas.exe",
        Arguments = "/user:Administrator cmd /K " + command
    };

    using (Process proc = new Process())
    {
        proc.StartInfo = procStartInfo;
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();

        if (string.IsNullOrEmpty(output))
            output = proc.StandardError.ReadToEnd();

        return output;
    }
}

Those who could not find the solution to their problems i have found this solution for me: On the solution file, choose

Add => New item => Application manifest file

then open it on C#.

在此输入图像描述

On application manifest file rename "asInvoker" as "requireAdministrator". At the end, application manifest file should look like:

在此输入图像描述

Now, build your solution. Then you will be able to open all the apps as administrator rights.

There is at least one problem with your command, this line:

Arguments = "/user:Administrator cmd /K " + command

Should be:

Arguments = "/user:Administrator \"cmd /K " + command + "\""

Also, this won't work as a fully automated process, because it will ask for the Administrator's password which in Windows Vista and newer is not known.

The UAC will pop up depending on the user settings in "User Account Control Settings." A program cannot bypass that. Only if the user has settings of "Never Notify" will your program do what you are trying to do.

Two solutions: First, you may the user's appdata directory. This will save you from needing admin privileges in the first place. (in a more general approach - think carefully if you really need those privileges)

Another solution us creating a Windows service which would have those privileges. The first installation of that service would require admin privileges, but after that, you can delegate your work to that service.

The second solution is a potentially security breach - so you'll have to think about what that service would be able to do carefully.

I have been using this code:

        string[] commands = File.ReadAllLines(commandFile);
        foreach (string command in commands)
        {
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            //startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.WorkingDirectory = @"C:\Windows\System32";
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/user:Administrator \"cmd /K " + command + "\"";
            process.StartInfo = startInfo;
            process.Start();
        }

As you can see: Trying this code from 'Run' in VS will not give admin, But if you compile this program and run it externally as admin it will. I used this batch file to test privilege level.

@echo off
goto check_Permissions

:check_Permissions
echo Administrative permissions required. Detecting permissions...

net session >nul 2>&1
if %errorLevel% == 0 (
    echo Success: Administrative permissions confirmed.
) else (
    echo Failure: Current permissions inadequate.
)

pause >nul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM