I am new to C# programming. I would like to have an application contains 1 text box and 1 submit button to exec a batch file.
The file is d:\XMLupdate.bat, but the program appends a number on the command line to the file for example d:\XMLupdate.bat 10 or d:\XMLupdate.bat 15
Another thing is that the submission has to be validated to either 1 -999 or ALL
In Java way:
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.");
}
}
However, the machine can not install JVM. Therefore, i have to build it in exe. My programming language is C#:
Here is the source code:
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");
}
}
}
My main question is about process method, can i execute it like that? thanks
Instead of invoking cmd.exe
directly, you can set p.UseShellExecute
. This works with batch files, HTML files (launches the default browser), etc:
[NOTE: you need the @ sign in @"c:\temp"
so the backslash isn't treated as an escape character.]
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();
"can i execute it like that?"
- Does your solution compile?
- If so, what happens when you run it?
On thing I noticed off the bat is this line
p.StartInfo.Arguments = "xmlupdate.bat" + int.Parse(textBox1.Text);
You need a space between the batch file and the next argument.
p.StartInfo.Arguments = "xmlupdate.bat " + int.Parse(textBox1.Text);
Beside Eric's answer, I would suggest to use NumericUpDown control instead.
You'll probably want to hook standard output and error for the process as well, redirecting them so you get notified of any output produced. It's useful to log it.
You also might want to hook standard input as well, but only if you know what input its expecting (you don't get an event fired when the process wants input: you have preemptively provide it.)
So if your batch file/child process wants input and you can't provide it, the child process will hang forever.
精彩评论