如何从C#中一个完整的路径创建多个目录? [英] How to create multiple directories from a single full path in C#?

查看:756
本文介绍了如何从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 \)

与普遍看法相反,<一个href="http://msdn.microsoft.com/en-us/library/54a0at6s.aspx"><$c$c>Directory.CreateDirectory会自动创建任何父目录不存在。
 在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)

这篇关于如何从C#中一个完整的路径创建多个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆