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
153 views
in Technique[技术] by (71.8m points)

c# - Why does .NET add an additional slash to the already existent slashes in a path?

I've noticed that C# adds additional slashes () to paths. Consider the path C:Test. When I inspect the string with this path in the text visualiser, the actual string is C:\Test.

Why is this? It confuses me, as sometimes I may want to split the path up (using string.Split()), but have to wonder which string to use (one or two slashes).

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

The \ is used because the is an escape character and is need to represent the a single .

So it is saying treat the first as an escape character and then the second is taken as the actual value. If not the next character after the first would be parsed as an escaped character.

Here is a list of available escape characters:

' - single quote, needed for character literals
" - double quote, needed for string literals
\ - backslash
 – Null 
a - Alert 
 - Backspace 
f - Form feed 

 - New line 

 - Carriage return 
 - Horizontal tab 
v - Vertical quote 
u - Unicode escape sequence for character 
U - Unicode escape sequence for surrogate pairs. 
x - Unicode escape sequence similar to "u" except with variable length.

EDIT: To answer your question regarding Split, it should be no issue. Use Split as you would normally. The \ will be treated as only the one character of .


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

...