mis*_*312 13 c# string path backslash
我有一个路径,我想添加一些名为test的新子文件夹.请帮我看看如何做到这一点.我的代码是:
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Console.WriteLine(path+"\test");
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:"c:\ Users\My Name\Pictures\test"
请帮我找出正确的方法.
Ste*_*eve 30
不要尝试构建连接字符串的路径名.使用Path.Combine方法
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Console.WriteLine(Path.Combine(path, "test"));
Run Code Online (Sandbox Code Playgroud)
在Path类包含了许多有用的静态方法来处理包含路径,文件名和扩展名字符串.这个类对于避免许多常见错误非常有用,并且还允许在操作系统之间编写更好的可移植性(在win上为"\",在Linux上为"/")
Path类在命名空间中定义System.IO.
您需要添加using System.IO;到您的代码中
Moo*_*ice 20
你需要逃脱它. \t是Tabs的转义序列0x09.
path + "\\test"
或使用:
path + @"\test"
更好的是,让我们Path.Combine为你做脏事:
Path.Combine(path, "test");
Path驻留在System.IO命名空间中.