Link to home
Start Free TrialLog in
Avatar of jholmes724
jholmes724

asked on

Handling OS Path characters in a .NET TextBox so the double backslash does not appear after reference in code.

I am having a problem retreiving the path info when entered into a textbox.  For example.

I can read this path when referenced in code:   string myPath = @"C:\DATA\text.txt.

However I am not sure how to implement this when grabbing the string from input in a TextBox.

So it the user enters the same path in the TextBox   C:\DATA\text.txt.

Then I am not sure how to handle it.   I tried to concate the string with the @ char but that didn't work.  ie:  string myPath = "@" + textBox1.Text.ToString();

any ideas.

Thanks
Avatar of AlexFM
AlexFM

string myPath = textBox1.Text;

The problem of double backslash exists only in string constants in program code.
Avatar of jholmes724

ASKER

Not true, as I am debugging this app I can see the variable values of the textbox.  When I view them in debug mode the value of the TextBox.Text is clearly "C:\\DATA\\Text.txt".   This is why I am asking this question, how to I prevent that or easily elimiate the C# formatting if I can't use the @ to use the literal value.   I could write a bunch of code to evaluate the string and strip out the double backslashes but I am sure there is an easier way.
You can ignore these double backslashes.  They are the debugger's way of representing normal backslashes, and without doing so special characters such as tabs (\t) and CRLF (\r\n) would have to be displayed differently.  If you were actually to save one of these strings back to disk, it would be saved as "C:\DATA\Text.txt" even though the debugger displayed @"C:\\DATA\\Text.txt".
Ok, but in this scenario I am not writing to disk, I have to use this value in memory to do a comparison.  

For example I am comparing two String values:

string myString = "C:\Data\text.txt"

string myString2 = TextBox1.Text.   (User typed in C:\Data\Text.txt)

ok now if I compare the 2 strings

if (myString == myString2)

myString will be equal to "C:\Data\Text.txt"
But myString2 will be equal to "C:\\Data\\Text.txt:

So this will never resolve to true.

I just want to know, using C# code how to reference this textbox value in the same way I would using the @ char.   So I ask again.   How do I do this in code using the text value in the text box.  
ASKER CERTIFIED SOLUTION
Avatar of Expert1701
Expert1701

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial