Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
367 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 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

618 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