【发布时间】: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