0

I have trouble executing a command via c#. The command consists of two paths, which I have to combine. Both paths contain spaces.

        String arguments = "/K \"" + dtExecPath + "\"" + @" /f " + "\"" + tmpPackagePath + "\"";
       // arguments = AddQuotesIfRequired(arguments);
        Console.WriteLine("TEST: " + arguments);

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = @arguments;
        process.StartInfo = startInfo;
        process.Start();

When I execute the code, I get the following error: ""C:\Program" could not be found.

The generated command looks like this:

TEST: /K "C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\DTEXEC.exe" /f "C:\Users\toki\source\repos\Integration Services Project1\Integration Services Project1\tmp\O2Data (1).dtsx"

What's the error?

tobi
  • 753
  • 1
  • 14
  • 25

2 Answers2

0

This is not a C# problem. How arguments are parsed is up to the process you're starting (cmd), not the parent process.

Per the docs for cmd, you need exactly one set of quotation marks. What's not clear in the official docs is how to deal with nested quotation marks. This page indicates that you should double-quote the beginning and end; this seems to work for me:

String arguments = "/K \"\"" + dtExecPath + "\"" + @" /f " + "\"" + tmpPackagePath + "\"\"";
Jeff
  • 7,504
  • 3
  • 25
  • 34
0

I was able to solve the issue, by setting the startInfo.WorkingDirectory; Only then the process is executed without failure

tobi
  • 753
  • 1
  • 14
  • 25