尝试使用C#运行命令

3

我正在尝试编写一个程序,以自动化我的工作中新计算机的一些设置。其中之一的任务是更改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);
        }

1
你的第二个启动参数中"/c""power"之间没有空格。不过我好奇你为什么要在这里进行拼接。 - Jay
我刚刚加了空格,谢谢你。但是,我的消息框仍然没有返回任何内容。 - BigDevJames
runImport.Start(); 之前添加一个断点,查看即将执行的命令并手动运行它。 - Jay
我添加了断点并逐步运行,我应该寻找什么? - BigDevJames
如果您更改了输出 runImport.StandardError.ReadToEnd(),则必须相应地匹配 importCFG.RedirectStandardError = true。这就是为什么您会收到异常的原因。 - Alex
显示剩余5条评论
4个回答

2
MSDN文档建议在从流中读取后等待程序关闭。
        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("powercfg",
            string.Format("–import {0}", TargetPath))
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false,
        };

        System.Diagnostics.Process runImport = new System.Diagnostics.Process(importCFG);
        runImport.Start();
        string PowerCFGResult = runImport.StandardOutput.ReadToEnd();
        runImport.WaitForExit(); //Add this line--
        MessageBox.Show(PowerCFGResult);

然而,由于我没有你正在执行的应用程序,因此无法进行测试。

更新:我刚注意到你使用了@和转义字符串("\\"),这也可能是一个问题。


PowerCFGResult是一个字符串,不能在其后放置.WaitForExit();选项。我尝试在字符串声明之前放置runImport.WaitForExit();,但那也无效。 - BigDevJames
您也可以尝试直接执行命令,而不是通过命令提示符。 - Jay
我已经尝试了更新,但我的消息框仍然没有返回任何内容。虽然如此,我非常感谢你的帮助。在这种方法中,我尝试将命令行输出到一个字符串,我的想法正确吗? - BigDevJames
我刚看到你关于直接执行命令的评论,你能给我指一下如何做的教程吗?我找到了一个教程,教我如何这样做。抱歉,我有点新手。 - BigDevJames
是的,根据MSDN文档,你已经做得很正确了。我猜测命令端可能出了些问题。 - Jay

0

我尝试去掉不必要的反斜杠,但我的消息框仍然没有返回任何内容。 - BigDevJames

0

如果我理解正确,您正在尝试从网络位置复制到C:驱动器,我认为这需要提升您程序的权限。为了测试目的,请尝试从e:\ source复制到e:\ dest


文件已经成功复制,我已经验证过了。我拥有管理员权限,所以这不是问题,但还是谢谢您的关心。 - BigDevJames

0

文件复制得很好,我没有文件复制的权限问题,这是我最初关注的问题之一,但我确保文件复制得很好。 - BigDevJames
我只是在指出文件应该存储的良好方式上提出了抗议;管理员链接是关于从程序内部运行命令提示符的。我不认为它会读取位于C:\的文件,因为命令提示符不是管理员级别。至少这是我的理解。 - Bit
啊哈...我明白了,我得找个机会在不同的位置试一下。 - BigDevJames

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接