Search code examples
c#stringfilenamesstring-literals

Unwanted slash in string literal


I'm trying to pass a filename to a cmd prompt. Ive looked various places but cant seem to find the answer - I'm sure its simple.

My string is built like so:

string cmd = string.Format(@" -u{0} -p{1} -h{2} -P{3} {4} < ""{5}""",
                user, pass, server, port, database, filename);

However the result is a string like so:

-uuser -ppass -hhost -P1234 database < \"C:\\my file.sql\"

How do i remove the \ before and after the filename? Its causing the process to run with a file cannot be found error.

My string needs to be:

-uuser -ppass -hhost -P1234 database < "C:\\my file.sql"

EDIT: OK, disregard this question.. there is something else goind on with process.start();


Solution

  • I tried running the following code in Visual Studio and the output is correct. You are most likely looking at the debugging information in Visual Studio which will escape the quotes.

    string cmd = string.Format(@" -u{0} -p{1} -h{2} -P{3} {4} < ""{5}""",
        "Test", "Test2", "server", "1337", "db", "C:\\filename.sql");
    
    Console.WriteLine(cmd);
    Console.Read();
    

    The actual output is -uTest -pTest2 -hserver -P1337 db < "C:\filename.sql"