5,699,997 members and growing! (15,907 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » General     Intermediate

Directory.CreateDirectory() method bug fixed

By Sergey Okhotny

Fixing System.IO.Directory.CreateDirectory() security bug.
C#, Windows, .NET, Visual Studio, Dev

Posted: 19 Apr 2005
Updated: 19 Apr 2005
Views: 28,969
Bookmarked: 17 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.91 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 33.3%
3
1 vote, 33.3%
4
1 vote, 33.3%
5

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)
{
    // 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

About the Author

Sergey Okhotny


http://okhotny.googlepages.com
Occupation: Web Developer
Location: Ukraine Ukraine

Other popular Files and Folders articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
General[Message Deleted]memberandang3:58 9 Jun '05  
GeneralRe: A solution without using Scripting ObjectsussAnonymous8:58 13 Sep '05  
Generalsolution requires Full TrustsussAnonymous2:16 27 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Apr 2005
Editor: Smitha Vijayan
Copyright 2005 by Sergey Okhotny
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project