如何使用 C# 在 cmd 中以管理员身份 运行 多行?

How to run multiple lines in cmd as administrator using C#?

是否有解决方法或功能可以让我 运行 在一个提升的命令提示符下执行多个命令?

我尝试设置 UseShellExecute=false 并使用 StreamWriter,但我读到如果这样做,我将无法使用提升的命令提示符。

如果我设置 UseShellExecute=true,我可以使用 elevate cmd,但我需要使用 process.Argument(),它一次只能让我 运行 在一个 cmd 进程中使用一个命令。

我有一个循环,运行在不同的 cmd 进程中一次执行一个命令并且它有效。但是,当我每次在提升的 cmd 中请求我允许 运行 换行时都必须单击 'yes' 时,它会变得有点烦人。我宁愿只单击 'yes' 一次,让它自己 运行 行。

这是我目前得到的一部分:

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
        process.StartInfo = info;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        info.FileName = "cmd.exe";
        info.Verb = "runas";

        int rootDirSize = rootDir.Length;
        for (int i = 0; i < rootDirSize; i++)
        {
            string[] rootFile = Directory.GetFiles(@rootDir[i]);
            int rootFileSize = rootFile.Length;
            for (int j = 0; j < rootFileSize; j++)
            {
                if (rootFile.Contains("/elf") == true)
                {
                    info.Arguments = string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]);
                    //command.WriteLine(string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]));
                }
                else
                {
                    info.Arguments = string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -c {0} {1}", rootFile[j], rootFile[j]);
                    //command.WriteLine(string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -c {0} {1}", rootFile[j], rootFile[j]));
                }


                FileInfo fileSize = new FileInfo(rootFile[j]);
                long bytes = fileSize.Length;
                long mb = bytes / 1024;


                try
                {
                    process.Start();
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    process.WaitForExit();
                    timer.Stop();
                    var time = timer.Elapsed;
                    sw = File.AppendText(logFilePath);
                    {
                        sw.WriteLine(string.Format("{0, -60}{1, -45}{2}", rootFile[j], " Time: " + time, "Size: "+mb+" KB"));
                        sw.Close();
                    }
                }
                catch (Win32Exception ex)
                {
                    process.Close();
                }
            }
        }

在您的参数中将 user 指定为 Adminstrator

info.Arguments = "/user:Administrator \"cmd /K " + string.Format(@"/C ""C:\Program Files\MCSI\Responder\ddna.exe"" analyze -q -c {0} {1}", rootFile[j], rootFile[j]) + "\"";

运行 您的工具本身作为管理员,现在所有打开的 cmd.exe 实例都作为管理员运行。

为此,请将此条目添加到您的应用程序清单中:

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

现在,您的应用程序只获得一次 UAC 对话框。