【问题标题】:Running batch file & commands from c# Forms application从 c# Forms 应用程序运行批处理文件和命令
【发布时间】:2021-07-15 13:17:02
【问题描述】:

我有以下代码:

        private void RunBatchFile()
        {
            string batchFile = "FlashDevice.bat";
            string CurrentDir = Directory.GetCurrentDirectory();
            string logFile = "\""+ CurrentDir + "\\logFile.txt\" 2>&1 ";
            string[] lines =
            {
                "cd \"c:\\Users\\thebi\\esp\\esp-idf\" ",
               // " \"c:\\WINDOWS\\system32\\cmd.exe\" /k " +
                "\"c:\\Users\\thebi\\esp\\.espressif\\idf_cmd_init.bat\" \"c:\\Users\\thebi\\AppData\\Local\\Programs\\Python\\Python37\\\" \"c:\\Program Files\\Git\\cmd\\\"  > " + logFile,
                "cd " + projPath,
                "idf.py flash -b 921600 >> " + logFile
        };
            File.WriteAllLines(batchFile, lines);

            Process proc = null;
            try
            {
                string batDir = Directory.GetCurrentDirectory();
                proc = new Process();
                proc.StartInfo.WorkingDirectory = batDir;
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.CreateNoWindow = false;
                proc.StartInfo.Arguments = "/c /wait " + batchFile;

                proc.Start();
                proc.WaitForExit();

               // proc.Start();
               // proc.WaitForExit();
                MessageBox.Show("Bat file executed !!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }


           // File.Delete(batchFile);
        }

前几个命令运行良好,但似乎跳过了最后一个。如果我打开一个空白的命令提示符并从我创建的文件(FlashDevice.bat)中一次复制一个命令,那么一切正常。但是当我从 c# 程序运行它时,它似乎完全忽略了关键命令。

我也试过了:

        proc.StartInfo.FileName = batchFile;
        proc.StartInfo.CreateNoWindow = false;
        proc.StartInfo.Arguments = "/k /wait ";

没有区别。有经验的 c# 人能否帮助指出错误。谢谢。

【问题讨论】:

    标签: c# batch-file


    【解决方案1】:

    好吧,不管怎样,这似乎工作得很好,几乎没有那么多乱七八糟的东西:

        private void ProgramFirmware()
        {
            string CurrentDir = Directory.GetCurrentDirectory();
            string logFile = "\"" + CurrentDir + "\\logFile.txt\" 2>&1 ";
    
            string strCmdText =
                "'/c cd \"c:\\Users\\thebi\\esp\\esp-idf\" && " +
                " echo \"Setting Up environment...\" && " +
                "\"c:\\Users\\thebi\\esp\\.espressif\\idf_cmd_init.bat\" \"c:\\Users\\thebi\\AppData\\Local\\Programs\\Python\\Python37\\\" \"c:\\Program Files\\Git\\cmd\\\" > " + logFile + " && " +
                "cd " + projPath + "&& " +
                " echo \"Burning Device Firmware...\" && " +
                "idf.py flash -b 921600 >> " + logFile + "'";
            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 2022-01-05
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多