Click to See Complete Forum and Search --> : Escape character problem
shuvo
February 11th, 2009, 12:10 AM
I have file urls stored in a database field. Urls are like this:
"\\Images\Sunset.jpg"
Where I retrieve it and store in tha string variable it becomes
"\\\\Images\\Sunset.jpg";
@ character can be used with strings like strval=@"\\Images";
But how do I use it with string variables or Database values? Other than using Replace("\\","\"), can anyone suggest a better solution?
Krishnaa
February 11th, 2009, 12:59 AM
I don't think by just reading into string the escape chars would get added, I suspect that it is stored in DB that way.
jmedved
February 11th, 2009, 02:03 AM
Where I retrieve it and store in tha string variable it becomes
"\\\\Images\\Sunset.jpg";
How are you checking for this.
If you just look at variable with debugger, it always escapes it (even if you originally used @ char). If you are watching this through debugger, content is ok.
Put variable into TextBox or something similar, and you will see it without escaping.
foamy
February 11th, 2009, 02:05 AM
just because the string's value is
"\\\\Images\\Whatever"
When you're debugging, the printed value when you output it to the console or a textbox for example will still be
\\Images\Whatever
EDIT: D'oh, beaten to the punch! :)
shuvo
February 13th, 2009, 01:45 AM
Thanks to all of you for your input.I found out that url is still working with "\\\\", that is returned from the database. I just had to replace the last "\\" with a "/".Seriously .net should improves its handling of escape characters.
foamy
February 13th, 2009, 01:46 AM
Having to replace a backslash with a forward slash has nothing to do with .NET escape characters though ...
shuvo
February 13th, 2009, 02:26 AM
@Foamy...Try replacing "\\" with "\".;)
foamy
February 13th, 2009, 02:51 AM
I'm not sure what you mean... I single backslash will appear as "\\" in C# since it doubles as an escape character, so it needs to be escaped. Two backslashes will therefore appear as "\\\\".
This can be avoided by appending a @ to the front of the string like this:
string path = @"C:\Program Files\...";
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.