Click here to Skip to main content
16,022,536 members
Articles / Programming Languages / C#
Article

Directory.CreateDirectory() method bug fixed

Rate me:
Please Sign up or sign in to vote.
4.10/5 (4 votes)
19 Apr 2005 71.4K   24   4
Fixing System.IO.Directory.CreateDirectory() security bug.

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.

C#
public static void CreateDirectory(string DirectoryPath)
{
    // trim leading \ character
    DirectoryPath = DirectoryPath.TrimEnd(Path.DirectorySeparatorChar);
    Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
    // check if folder exists, if yes - no work to do
    if(!fso.FolderExists(DirectoryPath))
    {
        int i = DirectoryPath.LastIndexOf(Path.DirectorySeparatorChar);
        // find last\lowest folder name
        string CurrentDirectoryName = DirectoryPath.Substring(i+1, 
                                        DirectoryPath.Length-i-1);
        // find parent folder of the last folder
        string ParentDirectoryPath = DirectoryPath.Substring(0,i);
        // recursive calling of function to create all parent folders 
        CreateDirectory(ParentDirectoryPath);
        // create last folder in current path
        Scripting.Folder folder = fso.GetFolder(ParentDirectoryPath);
        folder.SubFolders.Add(CurrentDirectoryName);
    }
}

License

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


Written By
Web Developer
Ukraine Ukraine

Comments and Discussions

 
Generalcreate directory on remote Client Pin
Reza Madani26-Jan-09 10:57
Reza Madani26-Jan-09 10:57 
Dear Sir
Please guid me for create directory on remote client in Asp.net
regareds
Reza Madani

Reza Madani

General[Message Deleted] Pin
andang9-Jun-05 2:58
andang9-Jun-05 2:58 
GeneralRe: A solution without using Scripting Object Pin
Anonymous13-Sep-05 7:58
Anonymous13-Sep-05 7:58 
Generalsolution requires Full Trust Pin
Anonymous27-Apr-05 1:16
Anonymous27-Apr-05 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.