我正在尝试编写一个程序,以自动化我的工作中新计算机的一些设置。其中之一的任务是更改Windows 7的电源选项。我正在尝试运行一个命令来导入电源配置文件并将包含GUID的字符串输出返回到变量中。当我运行程序时,它没有返回任何值给我的字符串。我确定我搞错了什么,有人可以帮我看看吗?以下是我的代码:
if(chkPowerSettings.Checked == true && radioDesktop.Checked == true)
{
//Establish the path to the power configuration file
string DesktopFileName = "WTCPowerDesktop.pow";
string CFGFileSource = @"\\\\ops-data\\Apps\\Builds";
string TargetPath = @"c:\\";
//Creates the strings for the whole path
string source = System.IO.Path.Combine(CFGFileSource, DesktopFileName);
string destination = System.IO.Path.Combine(TargetPath, DesktopFileName);
//Copies the file, the true will overwrite any already existing file
System.IO.File.Copy(source, destination, true);
System.Diagnostics.ProcessStartInfo importCFG = new System.Diagnostics.ProcessStartInfo("cmd","/c" + "powercfg –import C:\\WTCPowerDesktop.pow");
importCFG.RedirectStandardOutput = true;
importCFG.UseShellExecute = false;
importCFG.CreateNoWindow = false;
System.Diagnostics.Process runImport = new System.Diagnostics.Process();
runImport.StartInfo = importCFG;
runImport.Start();
string PowerCFGResult = runImport.StandardOutput.ReadToEnd();
MessageBox.Show(PowerCFGResult);
}
"/c"
和"power"
之间没有空格。不过我好奇你为什么要在这里进行拼接。 - JayrunImport.Start();
之前添加一个断点,查看即将执行的命令并手动运行它。 - JayrunImport.StandardError.ReadToEnd()
,则必须相应地匹配importCFG.RedirectStandardError = true
。这就是为什么您会收到异常的原因。 - Alex