繁体   English   中英

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

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

是否有变通办法或功能可以让我在一个提升的命令提示符中运行多个命令?

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

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

我有一个循环,可以在不同的cmd进程中一次运行一个命令,并且可以正常工作。 但是,每次我要我允许在提升的cmd中运行新行时,都必须单击“是”,这有点令人讨厌。 我宁愿单击一次“是”,然后让它自己运行。

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

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

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

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

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

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

在您的参数中,将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]) + "\"";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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