2013-03-13 59 views
0

我有什麼,我認爲是一個簡單的問題。傳遞用戶輸入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或其他方式來獲得數據的任何想法用戶進入那個特定的屬性?

在此先感謝您的幫助!

+0

爲什麼不把一個'ASP:TextBox'在頁面上,給它一個名字(比如'txtArguments'),然後只用'MyProcess.StartInfo.Arguments = txtArguments.Text'來獲取文本呢? – 2013-03-13 19:43:50

回答

0

在頁面創建一個TextBox,給它一個名稱,如argumentsTextBox和代碼它想:

MyProcess.StartInfo.Arguments = argumentsTextBox.Text; 
+0

工作就像一個魅力我的朋友。非常感謝,我無法告訴你我爲這個世界掃地了多久。 – ebeniezer 2013-03-13 20:28:19

0

我不知道你的用戶輸入的意思,但我會盡力的回答我C#經驗 - 在運行時接收用戶輸入:
在我的應用程序,我創建,其執行文件的過程。我處理進程的IO,並且當需要輸入時,執行模塊使用該處理程序。
它是WPF,所以執行模塊只知道我的應用程序實現的接口。當需要用戶輸入時,會顯示一個彈出窗口。