Reputation: 12517
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?
Upvotes: 4
Views: 1671
Reputation: 1010
You can also use a forward slash in windows for this. This would remove the need for escaping the backslash.
Upvotes: 1
Reputation: 4897
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 '\'.
Upvotes: 3
Reputation: 498924
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.
Upvotes: 15