user4106887
user4106887

Reputation:

How to add directory location to mysql varchar?

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

Answers (3)

Fritz.S.
Fritz.S.

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

jaywayco
jaywayco

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

ApceH Hypocrite
ApceH Hypocrite

Reputation: 1113

Just replace all slashes with double-slashes:

string input = @"C:\SomeFolder\SubFolder\image.jpg";
string query = input.Replace(@"\", @"\\");

Upvotes: 4

Related Questions