C#Windows窗体 - 分配从文本框中的文本在一个可变的XML文件中(C# Windows Fo

2019-10-18 15:31发布

我已经在Windows中工作表单在C#应用程序的运行我Phing构建文件的任务。

当我点击按钮,它执行phing构建文件(运行CMD),保存控制台输出到一个txt文件,并显示在一个文本输出。

问题是我有需要的用户的输入,像SVN提交需要用户输入提交信息的一些任务。

当我执行COMMIT任务,是表现出对用户的空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一个mensagem不提交”,答案被分配到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);

我的老板告诉我,有没有被这样的问题,只是想补充一点别人。当控制台关闭我想将所有的控制台输出到一个文本框或只是迫使控制台,直到我关闭它继续开放

Answer 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输出。 您可以直接使用事件读取过程的输出。



文章来源: C# Windows Forms - Assigning the text from a textbox in one variable within xml file
标签: c# xml textbox