Search code examples
c#cmdarduinoprocessstartinfo

Why does execution of arduino-cli via cmd with file names with spaces in path not work?


I'm making a C# application that compiles Arduino code with arduino-cli. I'm calling it with the Process class using the ProcessStartInfo class and of course via cmd.exe which is absolutely necessary.

arduino-cli.exe ignores all arguments and outputs the following two lines for five seconds on starting it directly instead of running it via cmd.exe or from within a PowerShell console:

This is a command line tool.

You need to open cmd.exe and run it from there.

I can select the directory with the correct path, but when I select another directory to be compiled, there is output by arduino-cli.exe the error message:

Error: unknown command "Studio" for "arduino-cli"

I think that is because the directory I'm selecting is within a folder called Visual Studio Projects containing spaces in its name and I think it interprets each word as separate argument.

How to code the arguments passed via cmd.exe to arduino-cli.exe so that the two file names (input and hex file) containing spaces in their full qualified file names as complete argument strings?

I read online that if I add @ before the path it should fix it, but it hasn't done much.

This also happens when I run the arduino-cli command line directly in a Windows command prompt window instead aside from C#. The issue is likely with the command line syntax.

Here is my C# code:

ProcessStartInfo cmd = new ProcessStartInfo();

cmd.FileName = "cmd.exe";
cmd.WindowStyle = ProcessWindowStyle.Normal;

hexFile = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\cache/a";

cmd.Arguments = "/k cd " + Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\avr-g++\\arduino-cli\\bin"
  + " & arduino-cli --compile " + @inFile + " --output " + @hexFile + " multi";
            
//file = hexFile;
Process.Start(cmd);

Solution

  • The issue was that the path was too long, and it needed a "" around the path. This is what the code looks like now -

     ProcessStartInfo cmd = new ProcessStartInfo();
    
     cmd.FileName = "cmd.exe";
     cmd.WindowStyle = ProcessWindowStyle.Normal;
    
     string name = GenerateName(8);
     hexFile = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\cache/" + name;
    
     cmd.Arguments = "/k cd " + Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\avr-g++\\arduino-cli\\bin"
       + " & arduino-cli compile -b arduino:avr:uno " + "\"" + @inFile + "\"" + " --build-path " + "\"" + @hexFile + "\"";
    
     file = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\cache\\" + name + "\\" + Path.GetFileName(inFile) + ".hex";
     Process.Start(cmd);
     Thread.Sleep(3000);
    ``