C # Use Process.Start with parameters and spaces in the path

I saw similar examples, but I can not find something like my problem.

I need to run a command like this from C #:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd";
startInfo.Arguments = "/K D:\\as df\\solver\\Swag.Console.exe -f D:\\as df\\solver\\2035.swag -p 5555";
Process.Start(startInfo);

does not work.

startInfo.Arguments = "/K \"D:\\as df\\solver\\Swag.Console.exe\" -f D:\\as df\\solver\\2035.swag -p 5555";

does not work.

startInfo.Arguments = "/K \"D:\\as df\\solver\\Swag.Console.exe\" -f \"D:\\as df\\solver\\2035.swag\" -p 5555";

does not work.

startInfo.FileName = "\"D:\\as df\\solver\\Swag.Console.exe\"";
startInfo.Arguments = " -f \"D:\\as df\\solver\\2035.swag\" -p 5555";

so it works, but I want CMD, is this possible?

enter image description here

+4
source share
1 answer

The / K argument will execute the following command. It may be a whole team related to and, but this is what you were missing in your attempts, they should be completely enclosed in double quotes in this case. See link SS64 cmd

In other words, the command should look like this:

cmd /K " here what ever you want to execute"

- . , String.Format, , . , , :

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd";
startInfo.Arguments = String.Format(
    "/K \"{0}\"", 
    " \"D:\\as df\\solver\\Swag.Console.exe\" -f \"D:\\as df\\solver\\2035.swag\" -p 5555");
Process.Start(startInfo);

Swag.Console.Exe , :

-f
D:\as df\solver\2035.swag
-p
5555
+3

Source: https://habr.com/ru/post/1664517/


All Articles