I have the following code:
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);
}
It runs through the first few commands fine, but seems to skip the last one. If I open a blank command promt and copy the commands in one at a time from the file I created (FlashDevice.bat) it all works fine. But when I run it from the c# program, it just seems to completely ignore the crucial command.
I also tried:
proc.StartInfo.FileName = batchFile;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.Arguments = "/k /wait ";
No difference. Could a more experienced c# person help point out the mistakes. Thanks.
Well, for what it's worth, this seems to work perfectly, without nearly as much messing around:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.