自动从长路径创建目录
我有一组具有完全限定路径的文件(root/test/thing1/thing2/file.txt)。我想foreach
这个集合并将文件放入路径中定义的位置,但是,如果某些目录不存在,我希望它们能够自动创建。我的程序有一个默认的“放置位置”,例如 z:/
。 “放置位置”从空开始,因此在上面的示例中,第一项应自动创建创建 z:/root/test/thing1/thing2/file.txt
所需的目录。我该怎么做?
I have a collection of files with fully qualified paths (root/test/thing1/thing2/file.txt). I want to foreach
over this collection and drop the file into the location defined in the path, however, if certain directories don't exist, I want them to great created automatically. My program has a default "drop location", such as z:/
. The "drop location" starts off empty, so in my example above, the first item should automatically create the directories needed to create z:/root/test/thing1/thing2/file.txt
. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其中文件是
IDictionary
。where files is
IDictionary<string, object>
.创建指定路径下的所有目录和子目录
Creates all directories and subdirectories in the specified path
检查 IO 命名空间 (目录,Path),我想他们会帮助你
然后检查一下..
Check IO namespace (Directory, Path), I think they'll help you
Then check it..
当然,有适当的异常处理。
with suitable exception handling, of course.
我发现在执行开始时设置“默认位置”很有帮助,并且可以减少一些冗余代码(例如,
Path.Combine(defaultLocation,relativePath)
)。例子:
I've found setting the "default location" at the start of execution to be helpful and reduce a bit of redundant code (e.g.,
Path.Combine(defaultLocation, relativePath)
).Example: