Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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");

The result I'm getting is : "c:UsersMy NamePictures est"

Please help me find out the right way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

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"));

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)

The Path class is defined in the namespace System.IO.
You need to add using System.IO; to your code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...