4

I'm writing the following code to read in a file from the filepath given (Using VS2010 & C#):

    static void Main(string[] args)
    {
        string temp;
        string path = "C:\Windows\Temp\fmfozdom.5hn.rdl";
        using(FileStream stream = new FileStream(path, FileMode.Open))
        {
            StreamReader r = new StreamReader(stream);
            temp = r.ReadToEnd();
        }

        Console.WriteLine(temp);
    }

The compiler is complaining about the following line:

string path = "C:\Windows\Temp\fmfozdom.5hn.rdl";

It gives the message: Unrecognised escape sequence at \W and \T

What am I doing incorrectly?

3 Answers 3

15

You can use a verbatim string literal:

string path = @"C:\Windows\Temp\fmfozdom.5hn.rdl";

Either that, or escape the \ character:

string path = "C:\\Windows\\Temp\\fmfozdom.5hn.rdl";

The problem with your current code is that the \ is the escape sequence in the string and \W, \T are unknown escapes.

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

Comments

3

Change it to:

string path = "C:\\Windows\\Temp\\fmfozdom.5hn.rdl";

The reason is that it's interpreting the 'W' and 'T' as escape characters since you only used one '\'.

Comments

1

You can also use a forward slash in windows for this. This would remove the need for escaping the backslash.

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.