1

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

8
  • Are you trying to breakout the & in the cmd instance or the C# instance or both? What is the literal path of the file. Commented May 22, 2020 at 15:59
  • 1
    No there's a & in the path to the file i just want to pass it in as part of the arguments some reason i cant seem to escape the char. In a cmd window i would just do this cmd.exe /C ""C:\Here ^& Here\MyExe.exe"" and it works just not via a process start Commented May 22, 2020 at 16:01
  • Try this instead: var arguments = @"/C ""C:\Here ^& Here\MyExe.exe"""; Commented May 22, 2020 at 16:25
  • Thanks but Still returning the same exit code 1.. Commented May 22, 2020 at 16:27
  • Before the & try putting the * wildcard character and see if it works. So it would be var arguments = $"/C \"C:\\Here *\\MyExe.exe\""; Commented May 22, 2020 at 16:28

1 Answer 1

2

It works fine if the path is double-quoted:

var arguments = "/c \"\"C:\\Here & Here\\MyExe.exe\"\"";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.