3

仕事で新しいコンピューターのセットアップを自動化するプログラムを作成しようとしています。タスクの 1 つは、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);
        }
4

4 に答える 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);

ただし、実行中のアプリケーションがないため、これを自分でテストすることはできません。

@更新:文字列 ( ) を使用およびエスケープしていることに気付きました"\\"。これも問題になる可能性があります

于 2013-06-20T15:08:14.893 に答える
0

C:\ に直接保存するのではなく、C:\Users\Username\AppData に保存する必要があります。C:\ に書き込むことを選択した場合は、このhow-to-force-my-net-app-to-に従う必要がある場合があります。 run-as-administrator-on-windows-7を実行し、管理者権限を強制します。

于 2013-06-20T15:25:08.967 に答える
0

コメントできません...なので、回答として投稿します。

先頭に @ がある文字列には、二重の \ は必要ありません。私は TargetPath 変数について考えていますが、前のものについては知りません。

http://msdn.microsoft.com/en-us/library/362314fe(v=vs.110).aspx

于 2013-06-20T14:52:54.743 に答える