I have been trying to run a Windows command, utilising Atmel Studios atprogram utility, which is not exactly relevant but helps for context.
The following is my code, and the idea is that the output from the command is pushed into a rich text box. This is completed within the "AddTestJigString(output)" section.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string command = cmd();
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "CMD.exe";
startInfo.Arguments = "/c cd " + ((char)34) + Path.GetDirectoryName(Properties.Settings.Default.Preferredatprogram) + ((char)34) + " && atprogram.exe - t avrispmk2 - i isp - d ATtiny26 - v chiperase program -f 30 - 5004_U21_V0.7.hex write - fs--values 61F6";
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
AddTestJigString(output);
process.WaitForExit();
}
Key line being;
startInfo.Arguments = "/c cd " + ((char)34) + Path.GetDirectoryName(Properties.Settings.Default.Preferredatprogram) + ((char)34) + " && atprogram.exe - t avrispmk2 - i isp - d ATtiny26 - v chiperase program -f 30 - 5004_U21_V0.7.hex write - fs--values 61F6";
If i change this to just;
startInfo.Arguments = "/c cd " + ((char)34) + Path.GetDirectoryName(Properties.Settings.Default.Preferredatprogram) + ((char)34) + " && atprogram.exe";
I get the output of the help / list of available commands within my rich text box from atprogram.exe, so i am doing something right. However as soon as any parmeters are added, the output is totally blank.
Thanks for the above responses.
In response, the arguments for this command are defintitly correct, as it is in use in a production environment as a batch script. The issue is actually down to outputting the command to a rich text box or more basically redirecting the command output.
The following solution I managed to get working:
{
string comhex = Path.GetFullPath(Properties.Settings.Default.PreferredComHex);
return "/c atprogram -t avrispmk2 -i isp -d ATtiny26 -v chiperase program -f " + ((char)34) + comhex + ((char)34) + " write -fs --values 61F6 2>&1";
}
The key part being the "2>&1" that I have added to the end of the command string. This effectively tells the command to redirect both Standard Output (stdout) and Standard Error (stderr). With 2 being stderr and 1 being stdout.
For further reading / reference: