添加反斜线字符串 [英] Add backslash to string
问题描述
我有一个路径,我想给它添加一些新的子文件名为test。
请帮我找出如何做到这一点。
我的代码是:
I have a path and I want to add to it some new sub folder named test. Please help me find out how to do that. My code is :
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Console.WriteLine(path+"\test");
我得到的结果是:C:\Users\My Name\Pictures EST
The result I'm getting is : "c:\Users\My Name\Pictures est"
请帮我找出正确的方式。
Please help me find out the right way.
推荐答案
不要试图打造的路径名连接字符串。使用 Path.Combine 法
Do not try to build pathnames concatenating strings. Use the Path.Combine method
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Console.WriteLine(Path.Combine(path, "test"));
的
的Path类包含了许多有用的静态方法来处理包含路径,文件名和扩展名字符串。这个类是非常有用的,以避免许多常见错误,并允许为操作系统之间更好的便携性代码(在Win\,/在Linux上)
The Path class contains many useful static methods to handle strings that contains paths, filenames and extensions. This class is very useful to avoid many common errors and also allows to code for a better portability between operating systems ("\" on win, "/" on Linux)
Path类在命名空间中定义 System.IO
结果
你需要添加使用System.IO;
你的代码
The Path class is defined in the namespace System.IO
.
You need to add using System.IO;
to your code
这篇关于添加反斜线字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!