传递用户输入ProcessStartInfo.Arguments
问题描述:
我有什么,我认为是一个简单的问题。传递用户输入ProcessStartInfo.Arguments
我试图创建具有用户输入的文本框的碱性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
一个TextBox,userinput或其他方式来获得数据的任何想法用户进入那个特定的属性?
在此先感谢您的帮助!
答
在页面创建一个TextBox
,给它一个名称,如argumentsTextBox
和代码它想:
MyProcess.StartInfo.Arguments = argumentsTextBox.Text;
+0
工作就像一个魅力我的朋友。非常感谢,我无法告诉你我为这个世界扫地了多久。 – ebeniezer 2013-03-13 20:28:19
答
我不知道你的用户输入的意思,但我会尽力的回答我C#
经验 - 在运行时接收用户输入:
在我的应用程序,我创建,其执行文件的过程。我处理进程的IO,并且当需要输入时,执行模块使用该处理程序。
它是WPF
,所以执行模块只知道我的应用程序实现的接口。当需要用户输入时,会显示一个弹出窗口。
为什么不把一个'ASP:TextBox'在页面上,给它一个名字(比如'txtArguments'),然后只用'MyProcess.StartInfo.Arguments = txtArguments.Text'来获取文本呢? – 2013-03-13 19:43:50