c# - 窗口窗体执行批处理文件

标签 c# windows file batch-file execute

我是 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");
            }
        }

    }

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

最佳答案

您可以设置p.UseShellExecute,而不是直接调用cmd.exe。这适用于批处理文件、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();

关于c# - 窗口窗体执行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764309/

相关文章:

c# - WPF DataGrid 在编辑后更新单元格样式

c# - 在 asp.net 中单击按钮浏览到文件

Windows 上的 Python 2.7——打开文件太多

windows - 用于检查域中计算机名称的简单批处理脚本

python - 如何使类似文件的类与 "isinstance(cls, io.IOBase)"一起工作?

c# - 在运行时检查 Glass Mapper V3 类型

c# - WPF自定义控件依赖属性中未知对象的双向绑定(bind)问题

c++ - 如何绘制圆角窗口边框?

c - 从文件中读取c代码

c - 使用 fputc() 在文件中存储整数会更改整数格式