欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#目录操作示例(获取目录名称 获取子目录)

程序员文章站 2024-02-25 11:33:04
 复制代码 代码如下:using system;using system.collections.generic;using system.linq;using...
 
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
namespace humep.filewatcher
{
    /// <summary>
    ///
    /// </summary>
    public static class directoryhelper
    {
        /// <summary>
        /// 初始化目录
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static bool createdirectory(string directory)
        {
            if (string.isnullorwhitespace(directory))
            {
                return false;
            }
            if (directory.exists(directory))
            {
                return true;
            }
            var dirinfo = directory.createdirectory(directory);
            return dirinfo != null;
        }

        /// <summary>
        /// 根据当前日组合文件夹名称
        /// </summary>
        /// <param name="now"></param>
        /// <returns></returns>
        public static string builddatedirectoryname(datetime datetime)
        {
            return datetime.tostring("yyyymmdd");
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static string buildtodaydirectoryname()
        {
            return builddatedirectoryname(datetime.now);
        }
        /// <summary>
        /// 获取目录名称
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static string getdirectoryname(string directory)
        {
            if (!directory.exists(directory))
            {
                return string.empty ;// directoryhelper.createdirectory(directory);
            }
            return new directoryinfo(directory).name;
        }
        /// <summary>
        /// 获取目录文件夹下的所有子目录
        /// </summary>
        /// <param name="directory"></param>
        /// <param name="filepattern"></param>
        /// <returns></returns>
        public static list<string> findsubdirectories(string directory, int maxcount)
        {
            list<string> subdirectories = new list<string>();
            if (string.isnullorempty(directory))
            {
                return subdirectories;
            }
            if (maxcount <= 0)
            {
                return subdirectories;
            }
            string[] directories = directory.getdirectories(directory);
            foreach (string subdirectory in directories)
            {
                if (subdirectories.count == maxcount)
                {
                    break;
                }
                subdirectories.add(subdirectory);
            }
            return subdirectories;
        }
        public static list<string> findsubdirectories(string directory)
        {
            return directory.getdirectories(directory, "*", searchoption.alldirectories).tolist<string>();
        }
        /// <summary>
        /// 根据时间查询子目录
        /// </summary>
        /// <param name="directory"></param>
        /// <param name="maxcount"></param>
        /// <returns></returns>
        public static list<string> findsubdirectories(string directory, int maxcount, int days)
        {
            list<string> subdirectories = new list<string>();
            if (string.isnullorempty(directory))
            {
                return subdirectories;
            }
            if (maxcount <= 0)
            {
                return subdirectories;
            }
            string[] directories = directory.getdirectories(directory);
            datetime lasttime = datetime.now.adddays(-math.abs(days));
            foreach (string subdirectory in directories)
            {
                if (subdirectories.count == maxcount)
                {
                    break;
                }
                directoryinfo dirinfo = new directoryinfo(subdirectory);
                if (dirinfo.lastwritetime >= lasttime)
                {
                    subdirectories.add(subdirectory);
                }
            }
            return subdirectories;
        }
        /// <summary>
        /// 将原目录移动到目标目录
        /// </summary>
        /// <param name="sourcedirectory"></param>
        /// <param name="targetdirectory"></param>
        /// <returns></returns>
        public static bool movedirectory(string sourcedirectory, string targetdirectory)
        {
            if (string.isnullorempty(sourcedirectory) || string.isnullorempty(targetdirectory))
            {
                return false;
            }
            string lasemovedirectory = string.format("{0}\\{1}", targetdirectory, directoryhelper.getdirectoryname(sourcedirectory));
            while (directory.exists(lasemovedirectory))
            {
                lasemovedirectory = directoryhelper.rename(lasemovedirectory);
            }
            directory.move(sourcedirectory, lasemovedirectory);
            return true;
        }
        /// <summary>
        /// 重新生成新的文件路径
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static string rename(string filepath)
        {
            if (string.isnullorwhitespace(filepath))
            {
                return string.empty;
            }
            string lastdirname = directoryhelper.getdirectoryname(filepath);
            //重命名,则随机在原来文件名后面加几个随机数字进行组装成新的名字
            random random = new random(system.datetime.now.millisecond);
            string randomdata = random.next().tostring();
            //把原文件名的名字加上随机数,组装成新的文件名(避免重名)
            string newlastdirname = lastdirname + randomdata;
            string newdirpath = string.empty;
            newdirpath = filepath.substring(0, filepath.lastindexof("\\")) + "\\" + newlastdirname;
            //返回新的路径
            return newdirpath;
        }
        /// <summary>
        /// 获取指定目录下的所有文件和文件夹大小
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <returns>string,返回所有文件夹名字</returns>
        public static long getdirectorysize(string path)
        {
            long dirsize = 0;
            directoryinfo dir = new directoryinfo(path);
            foreach (fileinfo file in dir.getfiles())
                dirsize += file.length;
            foreach (directoryinfo subdir in dir.getdirectories())
                dirsize += getdirectorysize(subdir.fullname);
            return dirsize;
        }
    }
}