如何在一个字符串写入一个反斜线(\\) [英] How do i write a backslash (\) in a string

查看:576
本文介绍了如何在一个字符串写入一个反斜线(\\)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样写 C:在文本\\ Users \\用户名\\文档\\任务 。这是实际code。

I want to write something like this C:\Users\UserName\Documents\Tasks in a textbox. This is the actual code.

    txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\Tasks";

我得到的错误无法识别的转义序列

请我怎样写字符串中的反斜杠??

Pls how do i write a backslash in a string??

推荐答案

反斜线(\\)字符是用来指示其他特殊字符这样一个特殊的转义字符随着新线( \\ n ),标签( \\ t )或引号( \\)如果你要包括一个反斜杠字符本身,你需要两个反斜杠或使用 @ 逐字字符串:\\\\任务 @\\任务

The backslash ("\") character is a special escape character used to indicate other special characters such as new lines (\n), tabs (\t), or quotation marks (\"). If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: "\\Tasks" or @"\Tasks".

阅读 MSDN文档/ C#规格其中讨论正在使用反斜杠字符和使用逐字字符串的转义字符的文字。

Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

一般的来讲,大多数C#.NET开发人员倾向于构建文件时使用 @ 逐字字符串/文件夹的路径,因为它节省了他们的青睐不必编写双反斜杠所有的时间,他们可以直接复制/粘贴的路径,所以我会建议你在做同样的习惯得到的。

Generally speaking, most C# .NET developers tend to favour using the @ verbatim strings when building file/folder paths since it saves them from having to write double backslashes all the time and they can directly copy/paste the path, so I would suggest that you get in the habit of doing the same.

这一切都表示,在这种情况下,我真的建议你使用 Path.Combine 实用方法中的 @ lordkain的回答作为,那么你就不需要担心是否反斜线已经包含在路径和意外加倍向上的斜线或合并路径部件时完全忽略他们。

That all said, in this case, I would actually recommend you use the Path.Combine utility method as in @lordkain's answer as then you don't need to worry about whether backslashes are already included in the paths and accidentally doubling-up the slashes or omitting them altogether when combining parts of paths.

这篇关于如何在一个字符串写入一个反斜线(\\)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆