C#在字符串中传递引号

时间:2016-02-22 18:20:16

标签: c# string powershell

我正在尝试在字符串中传递引号。我很难制定代码。

path = path.Insert(0, @"\\ffusvintranet02\picfiles\temp\");
string format = "Set-UserPhoto ";
format += "" + user + "";
format += " -PictureData ([System.IO.File]::ReadAllBytes(";
format += "" + path + @"";
format += ")";

用户和路径是需要在AD命令的单引号内的变量。命令。我所拥有的不起作用。

4 个答案:

答案 0 :(得分:1)

Company2_Customers符号的用户\""的{​​{1}}

\'

答案 1 :(得分:1)

首先,使用string.format执行此类任务。其次,你必须逃避引号(但你不需要逃避单引号)。

双引号可以通过双引号或基于您正在使用的字符串文字类型的反斜杠进行转义:

var s = @"something "" somethin else ";  // double double quote here

var s2 = "something \" somethin else ";

现在,使用string.format,您的代码将变为:

 path = path.Insert(0, @"\\ffusvintranet02\picfiles\temp\");
 string format = string.format("Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(\"{1}\")", user, path);

 path = path.Insert(0, @"\\ffusvintranet02\picfiles\temp\");
 string format = string.format(@"Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(""{1}"")", user, path);

答案 2 :(得分:0)

 string format = "Set-UserPhoto "; format += "'" + user + "'"; format += " -PictureData ([System.IO.File]::ReadAllBytes("; format += "'" + path + @"'"; format += ")";

答案 3 :(得分:0)

我建议在here-string中使用字符串插值,如下所示,这将阻止您使用字符串连接和转义。

$format = @"
Set-UserPhoto " + user + " -PictureData ([System.IO.File]::ReadAllBytes(" + path + ")"
"@
相关问题