Reputation: 125
Basically I need to create all folders in this path "C:\Users\Desktop\Info\Clients\Data" But not even the base "Info" directory exists, and there are many other paths I need to create, What's the best way I can go about doing this?
Upvotes: 1
Views: 5514
Reputation: 435
I tried created some new folders using
Directory.CreateDirectory("C:\Users\Desktop\Info\Clients\Data")
but the directories created are not visible on the screen. However I did a search using the search facility in the file explorer tab and it can locate them, so they must exist. I also did a restart in case this made them appear, but no it did not.
Upvotes: 0
Reputation: 216293
The class Directory in the namespace System.IO has a method called CreateDirectory that, as from MSDN remarks, creates every directory mentioned in the path passed.
Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
So you just need
Directory.CreateDirectory("C:\Users\Desktop\Info\Clients\Data")
and all the directories will be created if they don't exist.
In this specific example, as explained in a comment above from Hans Passant, you should try to avoid to use an hard coded path. The enumeration Environment.SpecialFolder is a symbolic reference to numerous well known location on your hard disk. You could pass an element of this enumeration to Environment.GetFolderPath to get back a physical path on your hard disk
Dim userDesktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim fullPath = Path.Combine(userDesktop, "INFO\Clients\Data")
Directory.CreateDirectory(fullPath)
Upvotes: 7