I have tried something like this
string path= Server.MapPath("~") + "color\";
But its throwing an error
"New line in constant"
Is there any way to append "\" in a string ?
Use a verbatim string literal
string path= Server.MapPath("~") + @"color\";
or \\
string path= Server.MapPath("~") + "color\\";
The problem is that \ escapes the closing ", that's why this doesn't work:
string invalid = "color\"; // same as: "color;
However, you should really use the Path class and it's methods if you're building paths as codingbiz has already mentioned in his answer. It will make your code more readable and less error-prone
Path class.Path class in general. However, the question was "how to append \ in string", so the focus is on the exception, why it happens and how to prevent it. Note that it's not path-specific and Path.Combine would not prevent the exception.Use @ verbtaim in your string;
string path= Server.MapPath("~") + @"color\";
or use \\ without verbtaim;
string path= Server.MapPath("~") + "color\\";
Check out String literals from MSDN.
Path.Combinethen you don't have to worry about the path delimiter.