2011-01-21 169 views
3

我是C#編程新手。我想有一個應用程序包含1個文本框和1個提交按鈕來執行一個批處理文件。窗體執行批處理文件

的文件是d:\ XMLupdate.bat,但該程序附加命令行到文件 例如d多項:\ XMLupdate.bat 10或d:\ XMLupdate.bat 15

另一個事情是,提交已被驗證要麼1 -999或ALL

在Java方式:

Runtime rt = Runtime.getRuntime(); 
          Process pr = rt.exec(command); 
         } 
       else{ 
       try{ 
int boxNumber = Integer.parseInt(jTextField1.getText()); 
         if((boxNumber > 0) && boxNumber < 1000) 
          { 
           String arguments = jTextField1.getText(); 
           String command = "CMD /C start d:/XMLupdate.bat " + arguments; 
           Runtime rt = Runtime.getRuntime(); 

rt.exec(command); 

          } 
         else{ 
          JOptionPane.showMessageDialog(null, "Invalid value entered."); 
          } 
}catch(Exception e) 
         { 
          JOptionPane.showMessageDialog(null, "Invalid value entered."); 
         } 
        } 

然而,機器無法安裝JVM。因此,我必須在exe中構建它。我的編程語言是C#:

這裏是源代碼:

public partial class Form1 : Form 
    { 

     //private System.Windows.Forms.TextBox textBox1; //Input text 
     //private System.Windows.Forms.Button button1; //Sumbit button 

     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //get text 
      // string str_inputText = textBox1.Text; 
      if (!(textBox1.Text.Equals(""))) 
      { 
        if (textBox1.Text.Equals("ALL")) 
         { 
          try 
          { 

           Process p = new Process(); 
           p.StartInfo.FileName = "CMD.exe"; //Execute command 
           p.StartInfo.WorkingDirectory = "c:\temp"; //Diretory 
           p.StartInfo.Arguments = "xmlupdate.bat" + int.Parse(textBox1.Text); 
           p.Start(); 

           p.WaitForExit(); 

          } 

          catch (Exception ex) 
          { 

           Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()); 
           MessageBox.Show("Invalid value entered"); 

          } 
         } 

      } 

     } 

     private void textBox1_TextChanged(object sender, CancelEventArgs e) 
     { 


     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      //Check the input number, only allow from 0 until 1000 
      try 
      { 
       int numberEntered = int.Parse(textBox1.Text); 
       if (numberEntered < 1 || numberEntered > 10) 
       { 
        // e.Cancel = true; 

       } 
      } 
      catch (FormatException) 
      { 
       // e.Cancel = true; 
       MessageBox.Show("You need to enter a number between 1 and 999"); 
      } 
     } 

    } 

我的主要問題是關於處理方法,我可以執行它這樣呢?謝謝

+3

您的代碼是否存在特定的錯誤,或者您問自己是否能夠正常工作而無需親自嘗試? – JeffO 2011-01-21 22:11:21

回答

2

「我可以執行它嗎?」

  1. 您的解決方案是否編譯?
  2. 如果是這樣,當你運行它會發生什麼?

類的東西,我注意到了蝙蝠是這條線

p.StartInfo.Arguments = "xmlupdate.bat" + int.Parse(textBox1.Text); 

您需要的批處理文件和一個參數之間的空間。

p.StartInfo.Arguments = "xmlupdate.bat " + int.Parse(textBox1.Text); 
4

而不是直接調用cmd.exe的,你可以設置p.UseShellExecute。這適用於批處理文件,HTML文件(啓動默認瀏覽器)等:

[注:需要在@"c:\temp"所以反斜線不被視爲轉義字符的@符號]

 Process p = new Process(); 
     p.StartInfo.FileName = @"c:\temp\xmlupdate.bat"; 
     p.StartInfo.WorkingDirectory = @"c:\temp"; 
     p.StartInfo.Arguments = textBox1.Text; 
     p.UseShellExecute=true; 
     p.Start(); 
+0

+1我錯過了c:\ temp轉義。 – 2011-01-21 22:23:50

0

您可能想要爲流程掛鉤標準輸出和錯誤,並將它們重定向,以便您可以獲知生成的任何輸出。記錄它很有用。

您可能還需要掛鉤的標準輸入爲好,但只有當你知道什麼樣的輸入它的期待(你沒有得到一個時觸發的事件過程中要輸入:你已經搶先提供它)

因此,如果您的批處理文件/子進程需要輸入並且您不能提供它,則子進程將永遠掛起。