1

I am trying to pass quotes in string. I am having a hard time formulating the code.

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

The user and path are variables that needs to be inside single quotes for the AD Command. command. What I have isn't working.

Tash
  • 11
  • 3

4 Answers4

1

User \" for " symbol or \' for '

format += "\"" + user + "\"";
Valentin
  • 5,380
  • 2
  • 24
  • 38
1

First of all , use string.format for such tasks. Second you have to escape quotes ( but you dont need to escape single quotes).

Double quotes can be escaped by double quote or by backslash based on type of string literal you are using:

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

var s2 = "something \" somethin else ";

Now, using string.format, your code will turn into:

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

or

 path = path.Insert(0, @"\\ffusvintranet02\picfiles\temp\");
 string format = string.format(@"Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(""{1}"")", user, path);
vittore
  • 17,449
  • 6
  • 44
  • 82
0
 string format = "Set-UserPhoto "; format += "'" + user + "'"; format += " -PictureData ([System.IO.File]::ReadAllBytes("; format += "'" + path + @"'"; format += ")";
Muhammad Salman
  • 543
  • 4
  • 22
0

I would suggest using string interpolation within a here-string as follows, this will prevent you from having to use string concatenation and escaping.

$format = @"
Set-UserPhoto " + user + " -PictureData ([System.IO.File]::ReadAllBytes(" + path + ")"
"@
Cobster
  • 1,243
  • 10
  • 18