Timothy Lukas H.
Timothy Lukas H.

Reputation: 684

Reading path written in text box removes backslash

In my current project, the user writes a file path (Example: "C:\Data") into a Textbox. Then I read it with:

string PathInput = tbPath.Text;

And then send it into an SQL Insert Query.

If I then read the data from SQL, I get back: C:Data

So I tried to do:

string Path = PathInput.Replace(@"\", "\\");

So that it would double the \\, because when I enter C:\\Data I get C:\Data. But it looks like the \ get lost in Textbox and not in Database.

So, how can I read the TextBox without losing the \s?

Upvotes: 3

Views: 1169

Answers (1)

JLe
JLe

Reputation: 2904

Your replace doesn't actually replace anything:

PathInput.Replace(@"\", "\\");

Since you use an @ before the first string, you don't have to escape anything. But in the second string, you don't use @, meaning you have to escape characters in that string - that means you're replacing the \ with another \.

Change it to:

PathInput.Replace(@"\", @"\\");

Upvotes: 4

Related Questions