代码之家  ›  专栏  ›  技术社区  ›  Marcus J.Kennedy

带字符串的双引号无效

  •  2
  • Marcus J.Kennedy  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  7
  •   Selman Genç    7 年前

    逐字字符串只适用于第一部分,因为您正在关注 +

    startInfo.Arguments = $@"/C = ""{service}"" >nul 2>&1";
    
        2
  •  2
  •   SᴇM    7 年前

    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";
    
    推荐文章