2

I have tried to insert a dynamic string in a static string with double quote, also tried How to add double quotes to a string that is inside a variable? but nothing work in my case:

startInfo.Arguments = @"/C = """+service+""" >nul 2>&1";

service is dynamic string and i need this result:

"/C = "mystring" >nul 2>&1";

Without dynamic variable i use double quote and it work, and i need @ for static Path

"/C = ""static"" >nul 2>&1";

enter image description here

Marcus J.Kennedy
  • 680
  • 5
  • 22

2 Answers2

7

The verbatim string only applies to the first part because you are concetanating with +, you can try using string interpolation:

startInfo.Arguments = $@"/C = ""{service}"" >nul 2>&1";
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
2

if (your c# version < c#6) use string.Format() method:

startInfo.Arguments = string.Format(@"/C = ""{0}"" >nul 2>&1", service);

you still can use + if you want:

startInfo.Arguments = @"/C = """+ 111 +"\" >nul 2>&1";

or even:

startInfo.Arguments = @"/C = """+ 111 + @""" >nul 2>&1";
SᴇM
  • 7,024
  • 3
  • 24
  • 41