How about this?

 

public static bool CanCreateDir(string path)

{

    var @return = false;

    try

    {

        var actions = new List<Action>();

        var di = new System.IO.DirectoryInfo(path);

        while (true)

        {

            if (di.Exists)

            {

                @return = true;

                System.IO.Directory.CreateDirectory(path);

                break;

            }

            var diFullName = di.FullName; // Must use local copy of string
in lambda.

            actions.Add(() => System.IO.Directory.Delete(diFullName));

            di = di.Parent;

            if (di == null)

            {

                actions.Clear();

                break;

            }

        }

        actions.ForEach(a => a());

    }

    catch

    {

        @return = false;

    }

    return @return;

}

 

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Tom Rutter
Sent: Thursday, 17 June 2010 13:22
To: ozDotNet
Subject: How to validate directory path

 

Gday .net gurus

 

Can I please get some recommendations on how to check if a directory can be
created given a path if it already doesnt exist?

As an example something like this

 

public static bool CanCreateDir(string path) {

            if (System.IO.Directory.Exists(path)) {

                        return true;

            }

 

            //TODO - figure out if the directory can be created

}

 

Cheers,

Tom

Reply via email to