Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem escaping path & filename 1

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
According to my C-Sharp book, I ought to be able to escape filenames in two ways:

string myFile = "C:\\MyDir\\MyFile.txt"
-or
string myFile = @"C:\MyDir\MyFile.txt"

However I've tried both of these in my current VS2008 project, and both evaluate to "C:\\MyDir\\MyFile.txt". I've checked by hovering over the variable at a breakpoint and also by printing in the Command Window. What am I doing wrong here???
 
the only thing i see missing is the semicolon (;) to terminate the line.
yes the will both evaluate to C:\\MyDir\\MyFile.txt the @ symbol is simply a shortcut. are you recieving an exception, or jus trying to understand the language?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Also, if you are debugging and watching the strings, they will have the escaped characters in it.
 
Yes, sorry I left off the semi-colons; they exist in my code.

My question is, according to my C-Sharp book, those statements should both evaluate to single backslashes (i.e., valid paths). Thus both strings when printed in the Command Window should be:
"C:\MyDir\MyFile.txt"

I'm trying to open a file, but it's not working and I suspect that is because the string variables are outputting double-backslashes rather than single ones as they should be.
 
Thanks dseaver. So the extra backslashes display when I hover (or watch) over the variable during debugging, and also when I print the variable's value in the Command Window? How can I output or evaluate the variable in such a way as to see its actual value then?
 
Hm...nevermind - when I display the variable in a MessageBox it does indeed have the single backslashes. Seems odd to me that printing via the Command Window doesn't display the literal string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top