How do you put quotes around variables when using complex commands and C# interpolation?

160 Views Asked by At

I am trying to perform a Robocopy of a file. The command I'm using (below) works when the "filename" variable does not contain spaces. How can I write this command to ignore spaces in this variable?

System.Diagnostics.Process.Start("robocopy.exe", 
                   $@"X: ""C:\users\username\desktop\test"" {filename}").WaitForExit();

Please See my comments below (i.e.; dguth8)

1

There are 1 best solutions below

0
dguth8 On

I found the solution for this myself. Just in case anyone else is trying something similar, here's the command I had to use:

Note: Essentially, I had to combine the 3 arguments for Robocopy into a single string, then use the approach addressed in the other question "How do you handle spaces in variables when using C# interpolation?", meaning escaping quotes around the variable.

System.Diagnostics.Process.Start("robocopy.exe", $"X:\ C:\\users\\usersname\\Desktop\\FolderName \"{filename}\"").WaitForExit();