Solved

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

Posted on 2006-11-27
5
355 Views
Last Modified: 2008-01-16
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
0
Comment
Question by:jholmes724
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
string myPath = textBox1.Text;

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

Author Comment

by:jholmes724
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:Expert1701
Comment Utility
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".
0
 

Author Comment

by:jholmes724
Comment Utility
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.  
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 50 total points
Comment Utility
string myString = @"C:\Data\text.txt"  //or string myString = "C:\\Data\\text.txt"

string myString2 = TextBox1.Text;  //User typed, C:\Data\Text.txt

if (myString == myString2) //WILL BE TRUE
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now