我有一个我认为很简单的问题。
我正在尝试创建一个基本的 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
关于文本框、用户输入或其他方式将数据从用户获取到特定属性的任何想法?
在此先感谢您的帮助!