1

Phing ビルドファイルのタスクを実行するために、C# の Windows フォーム アプリケーションで作業しています。

ボタンをクリックすると、phing ビルドファイル (cmd を実行) が実行され、コンソール出力が txt ファイルに保存され、出力が 1 つのテキスト ボックスに表示されます。

問題は、ユーザーがコミット メッセージを入力する必要がある SVN コミットなど、ユーザーの入力が必要なタスクがいくつかあることです。

コミット タスクを実行すると、ユーザーがコミット メッセージを書き込むための空の cmd が表示されましたが、質問は表示されないため、ユーザーはコンソールに何を書き込む必要があるかを推測する必要があります。

質問とユーザーの回答用のテキスト ボックスを含む入力ボックスを作成しましたが、テキスト ボックスのテキストを xml ファイル内の変数に割り当てるにはどうすればよいですか? 英語のエラーがいくつかありますが、申し訳ありません...

編集:

私のビルドファイルには次のものがあります:

    <target name="commit" description="Executa Commit">

    <propertyprompt propertyName="commit_message" defaultValue="Actualizado atraves do Phing"
            promptText="Introduza a mensagem do Commit: " />

        <svncommit
        svnpath="${svn_path}"
        workingcopy="${local_dir}"
        message="${commit_message} " />

        <echo msg="Revisao do Commit numero: ${svn.committedrevision}"/>

     </target>

したがって、「Introduza a mensagem do Commit」というメッセージが表示され、回答が commit_message に割り当てられます。C# には入力ボックスがあり、テキストボックスのテキストを xml ファイルの「commit_message」の値にしたい

カミルの編集:

textBox1.Clear();
        var startInfo = new ProcessStartInfo("phing");
        startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3";
        startInfo.Arguments = "commit > log.txt";

        string pergunta = inputbox.InputBox("Qual é a mensagem do Commit ?", "Commit", "Mensagem Commit");
        // textBox1.Text = "Escreva o caminho de origem da pasta:";

        Process proc = Process.Start(startInfo);
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardInput = true;



        proc.WaitForExit();
        using (StreamReader sr = new StreamReader(@"C:\wamp\bin\php\php5.4.3\log.txt"))
        {
            textBox1.Text = sr.ReadToEnd();
        }

番号 2 をカミルに編集します。

その方法は機能しますが、結果はこれを行うだけで同じです

 var startInfo = new ProcessStartInfo("phing");
        startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3";
        startInfo.Arguments = "commit ";
        Process proc = Process.Start(startInfo);

私の上司は、このような問題はないと私に言いました。他に1つ追加したい..コンソールが閉じたら、すべてのコンソール出力を1つのテキストボックスに送信するか、コンソールを閉じるまで強制的に開いたままにしたい

4

1 に答える 1

1

イベントを使用Process.OutputDataReceivedして「質問」を取得できます(cmdスクリプトから)。

アプリケーション (cmd?) 入力にデータを入力する場合は、Process.StandardInput.WriteLine()メソッドを使用する必要があります。

ProcessC# コードを投稿していないため、 cmd スクリプトを開始するために使用しているのか、それとも何を使用しているのかわかりません。

後で編集/追加:

    textBox1.Clear();
    var startInfo = new ProcessStartInfo("phing");
    startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3";
    // startInfo.Arguments = "commit > log.txt"; // DONT PUT OUTPUT TO FILE
    startInfo.Arguments = "commit"; // we will read output with event

    string pergunta = inputbox.InputBox("Qual é a mensagem do Commit ?", "Commit", "Mensagem Commit");
    // textBox1.Text = "Escreva o caminho de origem da pasta:";

    Process proc = Process.Start(startInfo);
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardInput = true;


    // not like this
    // proc.WaitForExit();
    // using (StreamReader sr = new StreamReader(@"C:\wamp\bin\php\php5.4.3\log.txt"))
    // {
    //    textBox1.Text = sr.ReadToEnd();
    // }

    // add handler
    // this will "assign" a function (proc_OutputDataReceived - you can change name)
    // that will be called when proc.OutputDataReceived event will occur
    // for that kind of event - you have to use DataReceivedEventHandler event type
    proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);


// event handler function (outside function that you pasted)
// this function is assigned to proc.OutputDataReceived event
// by code with "+= new..."
// "sender" is an object in which event occured (when it occurs - "proc" will be available as "sender" here)
// "e" is an object with event parameters (data sent from process and some more)
public void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    // cast "sender" to Process type
    // "sender" is Process, but it has "object" type, 
    // you have to cast it to use .StandardInput.WriteLine() on "sender"
    Process callerProcess = (Process)sender; 

    MessageBox.Show(e.Data); // this will show text that process sent
    MessageBox.Show(e.Data.ToString()); // you may need to add ToString(), im not sure

    if (e.Data.StartsWith("Revisao do Commit numero"))
    {
        MessageBox.Show("Process is talking something about Revisao numero"); // :)
        callerProcess.StandardInput.WriteLine("Yes! Numero!"); // reply "Yes! Numero!" to process
    }
}

より経験豊富な人が私のコードに間違いを見つけた場合は、可能であれば修正してください。今はテストできません。

後で追加:

ファイルを使用して cmd 出力を保存する必要はありません。イベントを使用して、プロセスの出力を直接読み取ることができます。

于 2013-09-17T15:17:15.383 に答える