Introduction
CreateDirectory() method of System.IO.Directory class has some security bug. If account running the code has no read permissions in the root directory, System.IO.Directory.CreateDirectory() method will fail. And you will get such error:
Could not find a part of the path "<full directory path>"
It is because System.IO.Directory.CreateDirectory() checks folder existence from root to lowest folder. The code provided in this article performs the scan in the reverse way - from lowest to upper. Therefore, it won't fail unless it will get to some folder with no read permissions.
Using the code
To use the provided code, add Microsoft Scripting Runtime COM object to your project references.
public static void CreateDirectory(string DirectoryPath)
{
DirectoryPath = DirectoryPath.TrimEnd(Path.DirectorySeparatorChar);
Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
if(!fso.FolderExists(DirectoryPath))
{
int i = DirectoryPath.LastIndexOf(Path.DirectorySeparatorChar);
string CurrentDirectoryName = DirectoryPath.Substring(i+1,
DirectoryPath.Length-i-1);
string ParentDirectoryPath = DirectoryPath.Substring(0,i);
CreateDirectory(ParentDirectoryPath);
Scripting.Folder folder = fso.GetFolder(ParentDirectoryPath);
folder.SubFolders.Add(CurrentDirectoryName);
}
}
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here