1

我一直在 C# 中的 Windows 窗体应用程序中工作,以运行我的 Phing 构建文件的任务。

当我单击按钮时,它会执行 phing 构建文件(运行 cmd),将控制台输出保存到 txt 文件中,并在一个文本框中显示输出。

问题是我有一些需要用户输入的任务,例如需要用户输入提交消息的 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);

我的老板告诉我这样没有问题,只想添加其他东西..当控制台关闭时,我想将所有控制台输出发送到一个文本框,或者只是强制控制台保持打开状态直到我关闭它

4

1 回答 1

1

您可以使用Process.OutputDataReceived事件来获取“问题”(来自我假设的 cmd 脚本)。

如果你想将数据输入到应用程序(cmd?)输入 - 你必须使用Process.StandardInput.WriteLine()方法。

你没有发布你的 C# 代码,我不知道你是Process用来启动 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 回答