wasSharpNET – Rev 22

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////

using System.IO;

namespace wasSharpNET.IO.Utilities
{
    public static class IOExtensions
    {
        public static bool isRootedIn(this string path, string root)
        {
            // Path is empty and root is empty.
            if (string.IsNullOrEmpty(path) && string.IsNullOrEmpty(root))
                return true;

            // Path is empty but the root path is not empty.
            if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(root))
                return true;

            // The supplied root path is empty.
            if (string.IsNullOrEmpty(root))
                return false;

            // The path is empty and the root is not.
            if (string.IsNullOrEmpty(path))
                return true;

            DirectoryInfo p = new DirectoryInfo(path);
            DirectoryInfo r = new DirectoryInfo(root);

            return string.Equals(p.Parent?.FullName, r.Parent?.FullName) || isRootedIn(p.Parent?.FullName, root);
        }

        public static void Empty(this string directory)
        {
            DirectoryInfo dir = new DirectoryInfo(directory);

            foreach (FileInfo fi in dir.GetFiles())
            {
                fi.Delete();
            }

            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                Empty(di.FullName);
                di.Delete();
            }
        }
    }
}