4

I've tried for quite a long time to figure out whats going on but I've not found anything anywhere that someone besides me has ran into this issue.

I'm simply trying to hard code a path into a string. Easy stuff. Well for some reason

string fullPathSourceFile = @"c:\SQLSOURCE.txt";

is evaluating to c:\\SQLSOURCE.txt

I've tried everything to evaluated it to a single backslash remove the double quotes and it wont work. I even tried Replace(@"\\", @"\") and it has no affect. Anyone have any idea what's going on with my code that would force a double backslash when a single one should be evaluated? This is driving me nuts and it's so damn easy yet causing me a lot of frustration.

I'm then using the string variable below:

                using (StreamReader reader = new StreamReader(fullPathSourceFile))
                {
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        sqlDBsource = line.ToString();
                    }

                    reader.Close();
                }

Thanks to everyone for their input which helped my figure out what I was doing wrong. In Visual Studio (which is confusing) when you look at the value of a string in the debugger, it puts escapes in for you, so a double-backslash in a watch window or variable value popup is normal and does not mean there are actually two backslashes. When you mouse-over the variable or watch it in the watch window, click the magnifying glass icon at the right hand side of the tooltip/pane, this will show you the unescaped string at it would be printed to the console. Another way to display the actual results is: Console.WriteLine(the_problem_string); The issue I was having with the code is outside the scope of the post but the confusion of the results I was seeing from Visual Studio lead me to believe the string was the source of the problem when it wasn't.

Mike
  • 419
  • 1
  • 6
  • 22
  • where is the issue, it appears to be working as expected. – user1336827 Dec 23 '13 at 17:26
  • Umm... So what is the issue? I don't see a double backslash anywhere in your question. – ohiodoug Dec 23 '13 at 17:27
  • I just edited it to show the double backslash; the SO markup was hiding it. – Colin D Bennett Dec 23 '13 at 17:28
  • 20
    Are you seeing double-backslashes while debugging in Visual Studio? If so, note that when you look at the value of a string in a debugger, it puts escapes in for you, so a double-backslash in a watch window or variable value popup is normal and does not mean there are actually two backslashes. – TypeIA Dec 23 '13 at 17:29
  • 1
    @Mike, have you tried using a regular (not verbatim) string? Like `"c:\\SQLSOURCE.txt"`? Obviously you have to double the backslash, but then it should only be a single backslash in the content of the string. – Colin D Bennett Dec 23 '13 at 17:30
  • I included the use of the variable in my post. I've tried "c:\\SQLSource.txt" and it also evaulated to C:\\SQLSource.txt – Mike Dec 23 '13 at 17:35
  • David, then why can't it find the file in the path provided? I am looking in the debugger on VS. I've seen a single forward slash before. – Mike Dec 23 '13 at 17:36
  • When you mouse-over the variable or watch it in the watch window, click the magnifying glass icon at the right hand side of the tooltip/pane, this will show you the unescaped string at it would be printed to the console. Do a `Console.WriteLine(the_problem_string);` and see what happens. – Gusdor Dec 23 '13 at 17:36
  • Interesting. I figured it out and it ended up being something else that was the issue. I had no idea that it displays the non-literal and you had to hit he magnified glass to see the literal. That led me to believe the string was the problem when it wasnt. Thanks for your help guys. – Mike Dec 23 '13 at 17:53
  • 2
    @Mike post what the actual issue was and how you fixed it as an answer to help anyone else who might run into this issue :) – Saggio Dec 23 '13 at 18:48

1 Answers1

3

This was a weird one. So I removed the verbatim as suggested in the comments and it worked when I used the double backslashes in the string. For some reason the code did not like the verbatim string and was translating the backslashes incorrectly. This resolved the issue. If anyone runs in to this you may need to play with the verbatim/non-verbatim strings because in some circumstances the compiler prefers non-verbatim.

Mike
  • 419
  • 1
  • 6
  • 22
  • 5
    As an aside for future readers: I very strongly suspect that this *isn't* a compiler issue, and was actually a failure of diagnostics. Compiler bugs are very few and far between, but developers are *very frequently* confused by the Visual Studio debugger, thinking that there are more backslashes in a string than there really are. When in doubt, display the string with something like `Console.WriteLine`. The verbatim string literal `@"c:\SQLSOURCE.txt"` most certainly does *not* end up with a string object containing a double backslash. – Jon Skeet Mar 29 '17 at 15:41