Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typing filepath as a string in c#

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?

like image 878
user559142 Avatar asked Dec 01 '22 01:12

user559142


1 Answers

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.

like image 96
Oded Avatar answered Dec 05 '22 15:12

Oded