Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create a couple of folders in a console app. some folders have two or three subfolders, and some have one subfolder, here is the folder structure;

Survey
    Photos
    Forms
    Report
Install
   Photos
Design
    Initial drawing
Inspection
Permit
    Approval permit
Proposal


I don't want to hardcoded in app, any suggestion on how I can handle it nicely. Thanks
Posted
Updated 19-Sep-14 16:48pm
v2
Comments
BillWoodruff 20-Sep-14 1:28am    
The key question here is whether you will always want the same folder structure shown above, or whether you want to write code to interpret some representation of a folder/sub-folder structure that may change.

So, where does the folder and sub-folder list come from: is it typed in by the user in the Console ?

I'd guess ... since this is a Console app ... that the folder/sub-folder structure will be different each time.

If the folder structure is always identical to that shown above, then I suggest you accept Garth Lancaster's solution below.

Another question ... along the same lines ... is whether you will always have only one level of nesting of subFolders, as you show in your example.

If the source is a single string as shown above, you need to deal with variable depths of nesting, like:

folder1
    subfolder1a
       subfolder1a1
folder2
folder3
    subfolder3a
       subfolder3a1
       subfolder3a2

then that requires will require recursive parsing of the coded representation ... unless all file paths are written out in full so that CreateDirectory's recursive creation capability can be used.

Im not sure what you mean by
Quote:
I don't want to hardcoded in app,


Does that mean you are ruling out

C#
Directory.CreateDirectory(@"Survey\Photos");
Directory.CreateDirectory(@"Survey\Forms");
Directory.CreateDirectory(@"Survey\Report");
Directory.CreateDirectory(@"Install\Photos");



??

you could of course store the paths as strings in a db, JSON, xml etc and loop through them, but that's still 'hardcoding'

One alternative would be
1) create the directory structure on disk
2) zip up the directory structure
3) include the zipped directory structure as a resource in your application
4) when the program runs, it could check if the directory structures exists, if not, get the resource zip and unzip it to the 'root' where you need it
 
Share this answer
 
Comments
BillWoodruff 20-Sep-14 2:11am    
If the OP confirms the Directory structure doesn't vary each time the Console app is run, you get my #5.
Member 1284721 24-Sep-14 23:09pm    
thank you for the solution, I will use xml and loop through them....
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx[^]

Directory.CreateDirectory(@"C:\Survey\Photos")

And it is always better to check whether the directory is already exist or not

C#
if (!IO.Directory.Exists(@"C:\Survey\Photos")
{
  IO.Directory.CreateDirectory(@"C:\Survey\Photos")
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900