-1

I am trying to pass a filepath to xcopy command for copying a folder from one location to another( CodedUI using C#). While doing the same the problems is, I am trying to add double quotes around the path but it's not taking the correct path format.

Code: 
string Path = "Some path to folder location";

// Tried all these solutions

Path = '\"' + Path + '\"';
Path = '\"' + Path + '\"';
Path = string.Format("\"{0}\"", Path );

Expected: ""Some path to folder location"" Actual:"\"Some path to folder location"\"

Please help.

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
bankarpit
  • 1
  • 6

6 Answers6

0

To add double quote, you need to add '\' before ' " '.

Note that : if you are having '\' in path, you have to take care of it like below.

if path is "D:\AmitFolder"

string path = @"D:\AmitFolder"; 
//Or
path = "D:\\AmitFolder"
string str = "\"" + path + "\"";
Console.WriteLine(str);

here str will be "Some path to folder location"

Output:

enter image description here

as in above line we are adding "\"" string as prefix and "\"" as post fix of the main string.

Amit
  • 1,821
  • 1
  • 17
  • 30
0

In debugger you will see the backslash.

Sent your output to Console and you will see the result is good.

 string Path = "Some path to folder location";

 Path = "\"" + Path + "\"";
 Console.WriteLine(Path);
PepitoSh
  • 1,774
  • 14
  • 13
0

From what i understand, you want to see

"Some path to folder location"

when you print it. If so, do:

string path = "\"Some path to folder location\"";

or

string path = "Some path to folder location";
var finalString = string.Format("\"{0}\"", path);
Doruk
  • 884
  • 9
  • 25
0

Maybe you should try verbatim strings like @"the\path\to\another\location".
This is the best way to write paths without having to struggle with escape codes.

EDIT:
You can use double quotes in a verbatim string:
@"""the\path\to\another\location"""

Adam Calvet Bohl
  • 1,009
  • 14
  • 29
0

If you're trying to preserve two sets of double quotes, try building the string like so:

var path = "hello";
var doubleQuotes = "\"\"";
var sb = new StringBuilder(doubleQuotes)
    .Append(path)
    .Append(doubleQuotes);
Console.WriteLine(sb.ToString()); // ""hello""

Of course, if you want single quotes you simply swap doubleQuotes for singleQuotes = "\""; and get "hello".

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
0

While storing string values if any double quotes are to be added they need to be escaped using backshlash(\). Single quotes are used for character data. The following code should get the output needed.

Path = string.Format("\"{0}\"", Path);

Also I have created a small fiddle here.