0

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.

  • Please read the documentation for the used classes. In this case you should read documentation of [Process Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process) from top to bottom with following the links to used classes like [ProcessStartInfo Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo). It has the property __WorkingDirectory__ which makes the usage of Windows command processor command `cd` useless on being correct set by your C# application before starting the process. – Mofi Oct 05 '19 at 10:09
  • So with setting __WorkingDirectory__ the execution of `cmd.exe` is not needed anymore. It is possible to run directly from within your C# application the executable `atprogram.exe` on specifying it with full qualified file name, i.e. drive + path + file name + file extension, see the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). – Mofi Oct 05 '19 at 10:12
  • In other words `startInfo.WorkingDirectory = Path.GetDirectoryName(Properties.Settings.Default.Preferredatprogram);` and `startInfo.FileName = "C:\\PathTo\\atprogram.exe";` and `startInfo.Arguments = "-t avrispmk2 -i isp -d ATtiny26 -v chiperase program -f \"30 - 5004_U21_V0.7.hex\" write -fs --values 61F6"` are the lines you really need in your C# code. I hope the list of `atprogram.exe` command line arguments is correct here. Your list is definitely wrong. You should read also the documentation of `atprogram.exe` which I don't have read for you. – Mofi Oct 05 '19 at 10:24
  • BTW: Starting Windows command processor `cmd.exe` with two commands to execute using `&&` would require for execution from within a command prompt window either `cmd.exe /C cd /D "Working directory path" ^&^& "C:\PathTo\atprogram.exe" -t avrispmk2 -i isp -d ATtiny26 -v chiperase program -f "30 - 5004_U21_V0.7.hex" write -fs --values 61F6"` or alternatively `cmd.exe /C "cd /D "Working directory path" && "C:\PathTo\atprogram.exe" -t avrispmk2 -i isp -d ATtiny26 -v chiperase program -f "30 - 5004_U21_V0.7.hex" write -fs --values 61F6""`. – Mofi Oct 05 '19 at 10:31
  • The help/documentation output on running `cmd /?` in a [command prompt](https://www.howtogeek.com/235101/) window explains how to use `cmd.exe` with option `/C` (run command line and close) or `/K` (run command line and keep running). `cd /?` outputs help/documentation of command `cd` and what option `/D` means (change also drive). See also the Microsoft documentation about [Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands). Yes, it is all documented by Microsoft. – Mofi Oct 05 '19 at 10:34

1 Answers1

0

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:

In the shell, what does " 2>&1 " mean?

https://support.microsoft.com/en-gb/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout