【问题标题】:C# Windows Forms - Assigning the text from a textbox in one variable within xml fileC# Windows 窗体 - 将文本框中的文本分配到 xml 文件中的一个变量中
【发布时间】:2013-09-18 00:53:09
【问题描述】:

我一直在用 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);

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

【问题讨论】:

  • 也许我误解了这个问题,但您是在问如何格式化您的 XML 吗?或者如何让代码与 XML 一起工作?我很困惑
  • 我在问如何将 c# 中文本框的值传递给一个 xml 文件中的一个变量。我想我需要使用 xmldocument 或类似的东西,但无法弄清楚...
  • 在 XML 文件中不存在 变量。变量是一种编程结构。 XML 文件是数据的结构化文本表示。您是要 a) 从 XML 文件读取还是 b) 写入 XML 文件?什么是 XML 架构?你想达到什么目的?您是使用 TortoiseSVN 作为客户端,还是使用普通的 svn -commit? AFAIK,要指定提交日志消息,您不需要创建 XML 文件,而只需创建一个包含数据的文本文件,或者您可以在命令本身中指定消息。
  • 看看这个答案是否有帮助stackoverflow.com/questions/1477075/…
  • Groo 我已经添加了一些代码,你现在能更好地理解它吗?请对不起我的英语...

标签: c# xml textbox


【解决方案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 输出。您可以使用事件直接读取流程输出。

【讨论】:

  • 我在上面的代码中更改了“startInfo.Arguments”。我忘了这件事。现在它应该可以工作了。
  • 在“proc_OutputDataReceived”中出现错误“方法必须有返回类型”
  • 已修复(通过在函数名称前添加“public void”)。
  • 好吧,我想你只需要学习如何使用 Process.StandardInput 和那个事件......做一些研究......
猜你喜欢
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2019-06-17
  • 2017-07-21
相关资源
最近更新 更多