如何从C#中一个完整的路径创建多个目录?

如何从C#中一个完整的路径创建多个目录?

问题描述:

如果你有一个完整路径,如:C:\ DIR0 \ DIR1 \ DIR2 \ DIR3 \ dir4 \你将如何最好地实现它,以便所有的目录是present?

If you have a full path like: "C:\dir0\dir1\dir2\dir3\dir4\" how would you best implement it so that all directories are present?

是否有这个在首创置业的方法?如果不是,什么是最优雅的方式来做到这一点?

Is there a method for this in the BCL? If not, what's the most elegant way to do this?

我会叫 Directory.CreateDirectory(@C:\ DIR0 \ DIR1 \ DIR2 \ DIR3 \ dir4 \)

与普遍看法相反,Directory.CreateDirectory$c$c>会自动创建任何父目录不存在。
 在MSDN的话,创建所有目录和子目录所指定的路径。

Contrary to popular belief, Directory.CreateDirectory will automatically create whichever parent directories do not exist.
In MSDN's words, Creates all directories and subdirectories as specified by path.

如果整个路径已经存在,它不会做任何事情。 (它不会抛出异常)

If the entire path already exists, it will do nothing. (It won't throw an exception)