带字符串的双块引用不起作用

时间:2018-10-04 10:50:34

标签: c# double-quotes

我尝试在带有双引号的静态字符串中插入动态字符串,也尝试了How to add double quotes to a string that is inside a variable?,但在我的情况下无济于事:

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

服务是动态字符串,我需要以下结果:

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

没有动态变量,我使用双引号并且它起作用,并且我需要 @ 作为静态路径

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

enter image description here

2 个答案:

答案 0 :(得分:7)

逐字字符串仅适用于第一部分,因为您正在使用+,可以尝试使用字符串插值:

startInfo.Arguments = $@"/C = ""{service}"" >nul 2>&1";

答案 1 :(得分:2)

if (your c# version < c#6)使用string.Format()方法:

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

如果需要,您仍然可以使用+

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

甚至:

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