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
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18019852
string myPath = textBox1.Text;

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

Author Comment

by:jholmes724
ID: 18020001
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
ID: 18020400
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
ID: 18020503
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
ID: 18020547
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

689 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