Search code examples
c#streamreader

read a path from file in a correct syntax with streamreader


I'm completely a junior here. I have tried something like

  • save a path and file string in a file like:

    c:\aaa\bbb\text.txt
    
  • then I need to read again as path but I get c:\aaa\bbb\text.txt from streamreader, but I need c:\\\aaa\\\bbb\\\text.txt

Can anyone help me?


Solution

  • I think you might be confusing string literals with a string.

    Say I write var myString = "\\" or var myString = @"\", this will show in the debugger as \\, because the debugger will format it as a literal. But if print it to the console, a file, or press the magnifying glass next to the string in the debugger, it will be shown as \, because that is the actual string value. See also verbatim string literal

    So, if you do myStreamWriter.Write("c:\\aaa\\bbb\\text.txt");, you will be actually saving the string c:\aaa\bbb\text.txt, and that is also the string that will be read back.

    However I fail to understand why you would want three slashes, I can only assume the OP thinks the escaping is done multiple times.