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
359 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 52
User Authentication using Digital Certificate 2 47
Cant save 3D 4 21
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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