Reputation:
When I do query and I add the location string example: "'C:\SomeFolder\SubFolder\image.jpg
'",
the query sets the value in database as "'C:SomeFolderSubFolderimagejpg
'" then I found that to save location you need to have two '\'
characters, (like this \\
) for it to be counted as a directory location, so can you make me a string function that you enter the location and the function returns the same string but with doubled backslashes?
input = 'C:\SomeFolder\SubFolder\image.jpg';
database need to save as 'C:\\SomeFolder\\SubFolder\\image.jpg';
Upvotes: 2
Views: 498
Reputation: 1
In C#, a single backslash needs to be written in a @-string or as a double backslash. The solution with @ is already given above. For the double backslash, it is as simple as:
string pathforsql = givenpath.Replace("\\\\", "\\\\\\\\");
Keep in mind \\\\
is only a notation, the real string contains just two backslashes.
Upvotes: 0
Reputation: 6296
Try:
public string EscapePathSeparators(string path)
{
StringBuilder sb = new StringBuilder();
foreach (var segment in path.Split(new char[]{ ‘\’ }, StringSplitOption.RemoveEmptyEntries))
{
sb.Append("\\" + segment);
}
return sb.ToString().Substring(2);
}
Upvotes: 0
Reputation: 1113
Just replace all slashes with double-slashes:
string input = @"C:\SomeFolder\SubFolder\image.jpg";
string query = input.Replace(@"\", @"\\");
Upvotes: 4