使用来自 C# 的参数以管理员身份运行 CMD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16529713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:14:13  来源:igfitidea点击:

Running CMD as administrator with an argument from C#

c#cmdinstallationadmin

提问by Eric Kim

I want to run cmd.exeas administrator with arguments from C#in order to prevent a UACpopup. 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 /qfor quiet installation.

我想以cmd.exe管理员身份运行C#中的参数以防止UAC弹出。这是必要的,以便将其用作自动安装过程。我传入的命令只是/q用于静默安装的安装文件 (.exe) 的路径。

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

当我运行这段代码时,有一个 CMD 弹出窗口,但它运行起来就好像它没有执行任何东西一样。

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;
    }
}

采纳答案by Rafael

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.

此外,这不会作为一个完全自动化的过程工作,因为它会要求管理员密码,而在 Windows Vista 和更新版本中,该密码是未知的。

回答by edtheprogrammerguy

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.

UAC 将根据“用户帐户控制设置”中的用户设置弹出。程序无法绕过它。只有当用户设置了“从不通知”时,您的程序才会执行您想要执行的操作。

回答by Shlomi Borovitz

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)

两种解决办法:首先,你可能是用户的appdata目录。这将首先使您免于需要管理员权限。(在更一般的方法中 - 如果您真的需要这些特权,请仔细考虑)

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.

另一个解决方案是我们创建一个具有这些权限的 Windows 服务。该服务的第一次安装需要管理员权限,但之后,您可以将您的工作委托给该服务。

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

第二种解决方案是潜在的安全漏洞 - 因此您必须仔细考虑该服务能够做什么。

回答by Shlomi Borovitz

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#.

然后在 C# 上打开它。

enter image description here

在此处输入图片说明

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

在应用程序清单文件中,将“asInvoker”重命名为“requireAdministrator”。最后,应用程序清单文件应如下所示:

enter image description here

在此处输入图片说明

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

现在,构建您的解决方案。然后您将能够以管理员权限打开所有应用程序。

回答by chemicalvamp

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.

如您所见:在 VS 中从“运行”中尝试此代码不会提供管理员权限,但是如果您编译此程序并以管理员身份在外部运行它,则会提供。我使用这个批处理文件来测试权限级别。

@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