如何在c#中使用字符串运行多个powershell命令?

时间:2016-06-30 23:39:54

标签: c# powershell

我曾经通过.Bat文件运行此程序。我试图看看我是否可以通过字符串来做到这一点。这个假设是关闭休眠,但不会这样做,并且以旧的方式工作。

旧代码:

newProcessInfo.Arguments = @"-executionpolicy unrestricted -Command "".\power\powercfg.bat""";

新代码:

private void button_Click(object sender, RoutedEventArgs e)
    {
     List<string> strings = new List<string>();
        strings.Add(" @echo off");
        strings.Add("set drive =% cd:~0,3 %");
        strings.Add("powercfg.exe - x - hibernate - timeout - dc 0");


        for (int i = 0; i < strings.Count(); i++) { 
            var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
        newProcessInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
        newProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        newProcessInfo.Verb = "runas";
        newProcessInfo.Arguments = @"-executionpolicy unrestricted -Command " + strings[i];
        System.Diagnostics.Process.Start(newProcessInfo);
        }
        MessageBox.Show("Power Config Settings Applied Successfully - Now Your PC Won't hibernate");
     }

0 个答案:

没有答案