0

Is there another way of assigning string path to variable aside from this:

strPath = @"C:\Myfile.txt";
  • is there another way instead using "@" sign in the string path.

thanks.

3
  • 2
    What's wrong with the @ sign? Commented Mar 9, 2010 at 3:47
  • in my situation i try not to specify the string path (hard coded). the string path will come from an openfiledialog, i pass it like this strPath = openfiledialog1.filename; when i run the program the error message was "cannot access "myFile.xls". Well i try to open it using microsoft.office.interop.excel library i thought the problem was on the code in opening the excel but when i try to hardcode the path using the @ sign it works. any suggestions? " Commented Mar 9, 2010 at 3:55
  • 3
    The @ sign has nothing to do with your problem; this question is irrelevant. You should ask a separate question about the exception. Commented Mar 9, 2010 at 4:06

5 Answers 5

4

You can escape it:

var myPath = "C:\\MyFile.txt"
Sign up to request clarification or add additional context in comments.

Comments

3

Do you mean another way of escaping the backslashes?

The @ sign at the start means that the string is treated as a verbatim string literal, and simple escape sequences such as \n or \t are ignored.

If you don't put the @ at the start it is not verbatim, and escape sequences are parsed. If you want to ignore an individual escape sequence you can precede it with a single backslash and it will be ignored.

The reason you would use it in a path such as your example is so that you don't have to escape each individual backslash as you would if you didn't put the @ at the start:

strPath = "C:\\Myfile.txt";

Comments

2

You can use forward slashes and it'll work fine on Windows and no escaping needed.

strPath = "C:/Myfile.txt";

Comments

1

You can use Unicode Escape Sequences....

 string strPath = "C:\u005CMyfile.txt";

Comments

0

string path = Path.Combine("C:", "myfile.txt");

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.