0

我有一个我认为很简单的问题。

我正在尝试创建一个基本的 Web 表单,它有一个用户输入文本框、一个运行进程的按钮和一个显示进程结果的标签或文本框。

诀窍是,我希望将用户输入用作我的代码中“StartInfo.Arguments”属性的值。

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace MyApp
{
public partial class InputTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void Button1_Click(object sender, EventArgs e)
    {
        Process MyProcess = null;
        string MyProcessOutput = null;
        string MyProcessInput = null;

        try
        {
            MyProcess = new Process();


            MyProcess.StartInfo.CreateNoWindow = true;
            MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            MyProcess.StartInfo.FileName = @"C:\Windows\sysWOW64\myapp.exe"; // Command to run
            MyProcess.StartInfo.Arguments = ...  //Where I would like to put user input
            MyProcess.StartInfo.UseShellExecute = false;
            MyProcess.StartInfo.RedirectStandardInput = true;
            MyProcess.StartInfo.RedirectStandardOutput = true;

            MyProcess.Start();


            MyProcessOutput = MyProcess.StandardOutput.ReadToEnd();
        }
        catch (Exception ex)
        {

            MyProcessOutput = "An error occurred:\r\n" + ex.Message;
        }
        finally
        {
            if (MyProcess != null)
                MyProcess.Dispose();
        }

        MyProcessOutput = MyProcessOutput.Replace("\r\n", "<br />\r\n");

        myresultsLabel.Text = MyProcessOutput;
    }
}

}

这里的关键是这一行:

  MyProcess.StartInfo.Arguments = ...  //Where I would like to put user input

关于文本框、用户输入或其他方式将数据从用户获取到特定属性的任何想法?

在此先感谢您的帮助!

4

2 回答 2

0

在页面中创建一个TextBox,给它一个名称,如argumentsTextBox,并将其编码为:

MyProcess.StartInfo.Arguments = argumentsTextBox.Text;
于 2013-03-13T19:43:49.113 回答
0

I'm not sure what you mean by user input but I'll try to answer from my C# experience - user input that is received at runtime:
In my app, I create a process which executes a file. I handle the process's IO, and when input is required, the executing module uses a handler for that.
It's WPF, so the executing module knows only an interface which is implemented by my application. When user input is required, a popup will show.

于 2013-03-13T19:45:00.757 回答