我是 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");
}
}
}
我的主要问题是关于过程方法,我可以这样执行吗?谢谢