BillyDay
BillyDay

Reputation: 47

C# escape & char in arguments path when running cmd.exe /C

Please could someone advise how I can escape an '&' character in a string when trying to start a process like the following:

            var arguments = $"/C \"C:\\Here & Here\\MyExe.exe\"";
            Process process = new Process
            {
                StartInfo =
                {
                    Arguments = arguments,
                    FileName = "cmd.exe",
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false
                }
            };
            process.Start();

I need to execute the process using cmd.exe /C I've tried replacing the string with to use a ^& like this:

var arguments = $"/C \"C:\\Here ^& Here\\MyExe.exe\"";

but it still doesn't find the path.

In a cmd window i would just do cmd.exe /C ""C:\Here ^& Here\MyExe.exe"" and it works just not via a process start

Thanks

Upvotes: 1

Views: 709

Answers (1)

Supa
Supa

Reputation: 486

It works fine if the path is double-quoted:

var arguments = "/c \"\"C:\\Here & Here\\MyExe.exe\"\"";

Upvotes: 2

Related Questions