Adding a string to the verbatim string literal

前端 未结 3 813
天涯浪人
天涯浪人 2021-01-05 01:20

I have a path that is named defaultPath I want to add it into this verbatim string literal but can quite get the quotes around it.

    @\"\"\"C:\\Mavro\\Mav         


        
3条回答
  •  粉色の甜心
    2021-01-05 01:53

    Do it like this (preferred):

    string.Format(@"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\{0}""", defaultPath);
    

    Or like this:

    @"""C:\Mavro\MavBridge\Server\MavBridgeService.exe"" /service /data ""..\" + defaultPath + "\"";
    

    The first one uses string.Format, which basically replaces the {0} in the first parameter with the value in the second parameter and returns the result.

    The second one uses classical string concatenation and what I did there was to remove the double quotes after the last backslash (""..\ instead of ""..\""), because you didn't want the quotes after the backslash. You wanted the quotes after defaultPath. And that's what this code does: It appends defaultPath (" + defaultPath) and appends the closing quote afterwards (+ "\"").

提交回复
热议问题