向字符串添加反斜杠 [英] 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+" est");
我得到的结果是:c:UsersMy NamePictures est"
The result I'm getting is : "c:UsersMy NamePictures 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
中.
您需要将 using System.IO;
添加到您的代码中
The Path class is defined in the namespace System.IO
.
You need to add using System.IO;
to your code
这篇关于向字符串添加反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!