反斜杠在我的网络服务中无法正常工作
我在 Web 服务中有一行简单的代码:
instance = @"\instanceNameHere";
但输出始终相同。
\\instanceNameHere
如果我删除@并使用两个斜杠,我会得到相同的结果。我以前从未见过这种情况,我的 Google fu 也让我失望了。我什至写了一个简单的应用程序,结果是正确的。那么为什么它会在网络服务中起作用呢?
I have a simple line of code in a web service:
instance = @"\instanceNameHere";
Yet the output is always the same.
\\instanceNameHere
If I remove the @ and use two slashes, I get the same result. I've never seen this before and my Google-fu has failed me. I even wrote a simple app and the result was correct. So why is it acting up in the web service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它在调试器中为您转义斜杠,因此您知道它是斜杠,而不是像
\t
这样的转义序列。如果调试器没有这样做,那么如何区分该字符串与调试器中的字符串
,因为后者在转义序列中由
\t
表示?因此,前者显示为,后者显示为
将其写入流或控制台,您将看到它只有一个斜杠,或者执行
instance.Length
并与字符数进行比较。您将在控制台上看到17
,而\\instanceNameHere
有 18 个字符。It's escaping the slash for you in the debugger so you know that it's a slash and not an escape sequence like
\t
. If the debugger did not do this, how could you distinguish the stringfrom the string
in the debugger since the latter is represented in an escape sequence by
\t
? Therefor the former is shown asand the latter as
Write it to a stream or the console and you'll see that it only has one slash, or do
instance.Length
and compare to a count of the characters. You'll see17
on the console, whereas\\instanceNameHere
has eighteen characters.调试器将字符串显示为 C# 文字。所以它用转义字符来显示它们。它还会将回车符显示为
\r
,将制表符显示为\t
。这纯粹是为了可视化——字符串实际上并不包含这些转义字符。如果将其写到日志中,它将不包含转义字符——它看起来会如您所期望的那样。The debugger displays strings as C# literals. So it's displaying them with characters escaped. It would also show carriage returns as
\r
and tabs as\t
. This is purely for visualization -- the string does not literally contain these escape characters. If you write it out to a log, it will not include the escape characters -- it will look as you expect.任何格式的 UNC 名称,始终以两个反斜杠字符 (“\”) 开头。
链接
更新:请参阅@Jason上面发帖!我没有意识到他正在检查调试器。
A UNC name of any format, which always start with two backslash characters ("\").
Link
Update : Please see @Jason post above! I didn't realise he was checking in the debugger.