Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - Strange path in debug is with double backslash \ but on the Message small magnifying glass it's with one backslash. What should I do?

When using a break point the root string contains the path with double backslash when I copy this complete path and try to get to it in the File Explorer the path can't be found. But if manual I remove one backslash from each part of the path so the whole path will be with one backslash instead two then it will find the path in the File Explorer. This is the problem in the code. I'm getting exception that the path can't be found but only because the path string is built for some reason with double back slash :

root

Then it's getting to a catch and there when I'm clicking on the small Message small magnifying glass I see the same root string but with one backslash. So I'm confuse why first in the break point it's with two backslash and in the catch with one backslash ? And how can I fix it so it will find the path with double backslash or with one only ?

The Message in the catch part same root string but with one backslash

I tried to add in the code this line :

Path.GetFullPath(root).Replace(@"", @"");

but it didn't change anything. Still, in the break point it's with two backslashes.

And most of the root paths are fine, but at some points, it's getting to the path in the screenshots and it's not finding this path because it's with two backslashes, but the path does exist and other paths are fine, so I can't figure out why this specific path can't be with two backslashes?

What should I do in this case/s ?

I also saw in some answers and solutions that doing this replace Replace(@"", @""); is not so good because some path's might be in the format E:\test est1 est2, which is a mix of two and one backslash.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When the breakpoint hits, it will show you the coding string representation, which will always show you a double backslash, which will always represent a single backslash; this is called an escape character:

"" =  // normal string representation
@"" =  // With the @ symbol, you only have to put down one backslash

Examples:

"
" // Represents a new line
""" // Represents the quote character (")
"u0031" // In debugging, will show you either "u0031" or "1", but visually will show you 1
""quotes"" // In debugging, will show you ""quotes"", but visually will show you "quotes"

You can find some more examples of escaped characters here.

In the second picture in your question, it only shows one backslash because it is the actual character (debugging shows you "", visually shows you ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...