自动从长路径创建目录

发布于 2024-09-29 05:13:04 字数 254 浏览 3 评论 0原文

我有一组具有完全限定路径的文件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾冷 2024-10-06 05:13:06
foreach (var relativePath in files.Keys)
{
    var fullPath = Path.Combine(defaultLocation, relativePath);
    var directory = Path.GetDirectoryName(fullPath);

    Directory.CreateDirectory(directory);

    saveFile(fullPath, files[relativePath]);
}

其中文件是IDictionary

foreach (var relativePath in files.Keys)
{
    var fullPath = Path.Combine(defaultLocation, relativePath);
    var directory = Path.GetDirectoryName(fullPath);

    Directory.CreateDirectory(directory);

    saveFile(fullPath, files[relativePath]);
}

where files is IDictionary<string, object>.

独闯女儿国 2024-10-06 05:13:06
string somepath = @"z:/root/test/thing1/thing2/file.txt";
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName( ( somepath ) );
string somepath = @"z:/root/test/thing1/thing2/file.txt";
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName( ( somepath ) );
神魇的王 2024-10-06 05:13:06
Directory.CreateDirectory("/root/...") 

创建指定路径下的所有目录和子目录

Directory.CreateDirectory("/root/...") 

Creates all directories and subdirectories in the specified path

弃爱 2024-10-06 05:13:06

检查 IO 命名空间 (目录Path),我想他们会帮助你

using System.IO

然后检查一下..

string fileName =@"d:/root/test/thing1/thing2/file.txt"; 
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
    Directory.CreateDirectory(directory);

Check IO namespace (Directory, Path), I think they'll help you

using System.IO

Then check it..

string fileName =@"d:/root/test/thing1/thing2/file.txt"; 
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
    Directory.CreateDirectory(directory);
溇涏 2024-10-06 05:13:06
string filename = "c:\\temp\\wibble\\wobble\\file.txt";
string dir = Path.GetDirectoryName(filename);
if (!Directory.Exists(dir))
{
    Directory.CreateDirectory(dir);
}
File.Create(filename);

当然,有适当的异常处理。

string filename = "c:\\temp\\wibble\\wobble\\file.txt";
string dir = Path.GetDirectoryName(filename);
if (!Directory.Exists(dir))
{
    Directory.CreateDirectory(dir);
}
File.Create(filename);

with suitable exception handling, of course.

勿忘初心 2024-10-06 05:13:06

我发现在执行开始时设置“默认位置”很有帮助,并且可以减少一些冗余代码(例如,Path.Combine(defaultLocation,relativePath))。

例子:

var defaultLocation = "z:/";
Directory.SetCurrentDirectory(defaultLocation);
Directory.SetCurrentDirectory(AppContext.BaseDirectory);

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:

var defaultLocation = "z:/";
Directory.SetCurrentDirectory(defaultLocation);
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文