-1

I have a textbox that has a value depends on the Path of the file after dragging the folder. the return value of dragged folder is "C:\Program Files". and I want to add the char "\" on the textbox, but if I add that character, there is an error such as red line below my code, the example of a red line is when you misspelled a word in a document.

code:

txtResult.Text + "\" + textFile + ".txt"

Question:

what is the main reason that this string value does have an error?

Alexis Villar
  • 371
  • 1
  • 3
  • 15

1 Answers1

3

When inside a string the backslash character '\', it is interpreted as an escape character.

In your case it's escaping the double quote, which is nesseccary when you want a double quote character inside a quoted string. However since you want the literal backslash, you should escape the backslash, which Means:

Use "\\".

Now it will produce a single backslash.

Edit:

Another option is to use an @-quoted string, like this:

@"\"

Now you don't have to escape the character.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57