Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development
Below is a simple EnsureDirectoryExists helper function:
public static void EnsureDirectoryExists(string targetPath)
        {
            string dir = Path.GetDirectoryName(targetPath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }
Posted on Sunday, May 17, 2009 5:58 PM | Back to top


Comments on this post: EnsureDirectoryExists helper function

# re: EnsureDirectoryExists helper function
Requesting Gravatar...
Two things:
1. Why are you doing that if statement? Directory.CreateDirectory will just return if the directory already exists, so just call CreateDirectory and let it do its thing.
2. Now you have the EnsureDirectoryExists method on all strings when that namespace is imported even though *most* of your strings aren't going to be filenames or paths (i'm assuming) which isn't really a problem, but would be an area of annoyance for me.
Left by Max Schmeling on Jun 04, 2009 12:41 AM

# re: EnsureDirectoryExists helper function
Requesting Gravatar...
For some reason I thought you were making an extension method... didn't look closely enough to see that there is no "this" in front of the parameter, so nevermind my issue #2. Issue #1 still applies though.
Left by Max Schmeling on Jun 04, 2009 1:18 AM

# re: EnsureDirectoryExists helper function
Requesting Gravatar...
Thanks,Max,
I wasn't aware, that CreateDirectory does nothing, if path already exists.
Anyway I like explicit name of my function, because without documentation people can expect thtowing exception from CreateDirectory.
Left by Michael Freidgeim on Jun 04, 2009 8:03 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free