Reputation: 95
I want to create a simple directory tree like this one, although it doesn't need to have the same name
Do I need to write it like this:
Directory.CreateDirectory("/home/XXX/Documents/Users/Pepe/datos/");
Directory.CreateDirectory("/home/XXX/Documents/Users/Juan");
Is there any option to create "Pepe" and "Juan" in the same string path?
Upvotes: 3
Views: 150
Reputation: 1
You can create like.
System.IO.Directory.CreateDirectory("c:/Dir1/Dir2/Dir3");
Think there is no Dir1 at root of c: drive.
It will create folder structure as like Dir1-->Dir2-->Dir3.
If Die1 is there then it will create rest of sub directory.
Same for multiple level.
Upvotes: 0
Reputation: 4963
It sounds like you have a common root directory:
/home/XXX/Documents/Users
and you want to add sub-directories from there. If that is the case and if the goal is to only have to reference the sub-directories, then I would go with @ColinM's approach, but modify it slightly as follows:
private static void CreateDirectories(string directoryRoot, params string[] directories)
{
foreach (string directory in directories)
{
Directory.CreateDirectory(Path.Combine(directoryRoot, directory));
}
}
Now when you call it, you only need to mention the root once
CreateDirectories("/home/XXX/Documents/Users", "Pepe/datos/", "Juan");
The nice thing about Path.Combine
is that you don't have to worry about having a slash /
that is either trailing in the directoryRoot
or leading in any of the directories
--it will make sure it is formatted correctly in the end.
Of course, if you take this approach, you'll need to decide what happens if directories
is null--do you create the directoryRoot (assuming it isn't already there), do you throw an error, or do you just return?
Upvotes: 1
Reputation: 2681
To post an answer and offer an alternative, if you only wanted to have one usage of the CreateDirectory
method then you could have a method that takes a parameter of params string[]
and pass in the directories there when calling the method.
The code would look like so
private static void CreateDirectories(params string[] directories)
{
foreach (string directory in directories)
{
Directory.CreateDirectory(directory);
}
}
You can call it with
CreateDirectories("/home/XXX/Documents/Users/Pepe/datos/", "/home/XXX/Documents/Users/Juan");
Or you can pass in a string array
CreateDirectories(new [] { "/home/XXX/Documents/Users/Pepe/datos/", "/home/XXX/Documents/Users/Juan"});
I reduce the usage of the System.IO
methods doing the above, then I interface the class which allows me to create mocks and then unit test the software I create without performing I/O operation, which brings one scenario where you could use the above except it wouldn't be static.
Upvotes: 3
Reputation: 35270
No there's nothing baked into the BCL to do that; you have to call CreateDirectory
for every one.
If you have a collection of directory paths, you can put that inside a loop:
foreach (string dirPath in directoryPaths)
{
Directory.CreateDirectory(dirPath);
}
Upvotes: 2