从Process.Start运行CMD无法启动

时间:2019-04-18 15:47:13

标签: c# batch-file

我需要从cd.bat运行此命令

powershell -windowstyle hidden -command "Start-Process cmd -ArgumentList '/c takeown /f \"C:\Windows\System32\wuaueng.dll\" && icacls \"C:\Windows\System32\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' -Verb runAs"

当我右键单击该BAT手动运行->以管理员身份运行时,当我尝试从CMD运行时,它运行完美:

Process.Start(Application.StartupPath + "\\cd.bat");

CMD启动但命令不起作用,为什么?

我的应用程序具有清单中的管理权限

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

我也尝试使用普通命令:

proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c powershell -windowstyle hidden -command \"Start - Process cmd - ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' - Verb runAs\"";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();

 var p = new Process();
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = @"C:\Windows\System32";
            p.StartInfo.Arguments = "/k powershell -windowstyle hidden -command \"Start - Process cmd - ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' - Verb runAs\"";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = false;
            p.StartInfo.UseShellExecute = false;
            p.OutputDataReceived += (a, b) => line = line + b.Data + Environment.NewLine;
            p.ErrorDataReceived += (a, b) => line = line + b.Data + Environment.NewLine;
            p.Start();
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();

            MessageBox.Show(line);

MessageBox结果为空,因此没有错误...

相同结果=(

1 个答案:

答案 0 :(得分:0)

解决了:

Process.Start("powershell.exe",
                 "-windowstyle hidden -command "
                + "Start-Process cmd -ArgumentList '/c takeown /f \"C:\\Windows\\System32\\wuaueng.dll\" && icacls \"C:\\Windows\\System32\\wuaueng.dll\" /grant *S-1-3-4:F /t /c /l' -Verb runAs"
            );