3

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 ?

6
  • 1
    What error? Just saying "thowing an error" is incredibly vague. Commented Jan 11, 2013 at 13:20
  • @DanielKelley:thanks for your feedback I have updated the question with error details Commented Jan 11, 2013 at 13:26
  • 3
    You might want to ask if you really need the \ at the end of the path string. If you get in the habit of always using Path.Combine then you don't have to worry about the path delimiter. Commented Jan 11, 2013 at 13:30
  • 1
    This question shows a lack of research on the author's part. This a basic problem of not using escape characters. Commented Jan 11, 2013 at 13:45
  • 2
    @Ramhound Cut him some slack. Maybe he never heard of escaping. Would you know how/where to search for a solution to "New line in constant" if you never heard of escaping? Commented Jan 11, 2013 at 15:38

6 Answers 6

11

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

Sign up to request clarification or add additional context in comments.

6 Comments

It's a good answer but I believe that the best one if @codingbiz's one because it makes use of Path class.
@MatíasFidemraizer: yes, it's good to use the 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.
@TimSchmelter Yes that's what matters first - solving the OP's problem at hand. And later suggesting alternatives.
@MatíasFidemraizer Tim was right too. My was extra stuff - may be needed
@TimSchmelter Yeah, but it's nice to show to code better in addition to solve the whole problem :D
|
4

Try this

string path = Path.Combine(Server.MapPath("~") + @"color\");

OR

string path = Path.Combine(Server.MapPath("~") + "color\\");

Path.Combine will make sure the path character "\" are inserted where missing

Comments

3

use @, verbatim string,

string path = Server.MapPath("~") + @"color\";

or double the \

string path = Server.MapPath("~") + "color\\";   

Comments

2

Escape it with another one.

string path= Server.MapPath("~") + "color\\";

Comments

2

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.

Comments

2

Use this

string path= Server.MapPath("~") + "color\\";

Or

string path= Server.MapPath("~") + @"color\";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.