首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xml Forms -将文本框中的文本赋给C#文件中的一个变量

xml Forms -将文本框中的文本赋给C#文件中的一个变量
EN

Stack Overflow用户
提问于 2013-09-17 22:55:55
回答 1查看 1.1K关注 0票数 1

我一直在使用C#的Windows Forms应用程序来运行我的Phing构建文件的任务。

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

问题是我有一些任务需要用户输入,比如需要用户输入提交消息的SVN commit。

当我执行提交任务时,显示了一个空的cmd,要求用户写入提交消息,但没有显示问题,因此用户必须猜测希望他在控制台中写入的内容。

我创建了一个输入框,其中包含问题和用户答案的文本框,但是如何将文本框中的文本赋给xml文件中的变量?对于一些英语错误我深表歉意。

编辑:

在我的构建文件中,我得到了以下内容:

代码语言:javascript
复制
    <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“的值

为Kamil编辑:

代码语言:javascript
复制
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编辑为Kamil:

这样做是可行的,但结果与这样做是一样的

代码语言:javascript
复制
 var startInfo = new ProcessStartInfo("phing");
        startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3";
        startInfo.Arguments = "commit ";
        Process proc = Process.Start(startInfo);

我的老板告诉我,这样是没有问题的,只想补充一件事。当控制台关闭时,我希望将所有控制台输出发送到一个文本框中,或者强制控制台保持打开状态,直到我将其关闭

EN

回答 1

Stack Overflow用户

发布于 2013-09-17 23:17:15

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

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

您没有发布您的C#代码,我不知道您是否正在使用Process启动命令脚本或其他什么。

稍后编辑/添加:

代码语言:javascript
复制
    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输出。您可以使用event直接读取流程输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18853190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档