微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

窗体执行一个batch file

我是C#编程新手。 我想有一个应用程序包含1个文本框和1个提交button来执行一个batch file。

文件是d: XMLupdate.bat,但程序会在命令行上追加一个数字,例如d: XMLupdate.bat 10或d: XMLupdate.bat 15

另一件事是,提交必须validation1至999或全部

以Java方式:

如何检查一个shell命令是否来自PHP

释放C ++资源和fork-exec?

父/子exception退出时保持分叉进程(C ++)

在具有IISconfiguration的Windows上执行PHP exec

如何用exec PHPclosuresUbuntu

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。 因此,我必须build立它在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"); } } }

我的主要问题是关于过程方法,我可以执行它吗? 谢谢

PHP exec中redirectSTDERR

在Linux上创buildPHP在线评分系统:exec行为,进程ID和grep

通过PHP的`exec`打开一个Windows应用程序

更新 – PHP的exec,系统或passthru都删除单引号或双引号

PHP exec()命令权限被拒绝

“我能像那样执行吗?”

你的解决方案是编译?

如果是这样,当你运行它会发生什么?

在我注意到的事情是蝙蝠是这条线

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

批处理文件和下一个参数之间需要一个空格。

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

不要直接调用cmd.exe ,你可以设置p.UseShellExecute 。 这适用于批处理文件,HTML文件(启动认浏览器)等:

[注意:在@"c:temp"需要@符号,所以反斜杠不被视为转义字符。]

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

除了埃里克的回答,我建议使用Numericupdown控制。

您可能还需要为流程挂钩标准输出错误,将其重定向,以便您可以获知生成的任何输出。 记录它是有用的。

你也可能想要挂钩标准输入,但是只有当你知道它的期望输入时(当进程需要输入时你不会得到一个事件:你已经预先提供了它)。

所以,如果你的批处理文件/子进程需要输入,而你不能提供它,子进程将永远挂起。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐