對目錄進行操作

CodeAgriculture發表於2013-09-25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Humep.FileWatcher
{
    ///
    ///
    ///

    public static class DirectoryHelper
    {
        ///
        /// 初始化目錄
        ///

        ///
        ///
        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;
        }
 
        ///
        /// 根據當前日組合資料夾名稱
        ///

        ///
        ///
        public static string BuildDateDirectoryName(DateTime dateTime)
        {
            return dateTime.ToString("yyyyMMdd");
        }
        ///
        ///
        ///

        ///
        public static string BuildTodayDirectoryName()
        {
            return BuildDateDirectoryName(DateTime.Now);
        }
        ///
        /// 獲取目錄名稱
        ///

        ///
        ///
        public static string GetDirectoryName(string directory)
        {
            if (!Directory.Exists(directory))
            {
                return string.Empty ;// DirectoryHelper.CreateDirectory(directory);
            }
            return new DirectoryInfo(directory).Name;
        }
        ///
        /// 獲取目錄資料夾下的所有子目錄
        ///

        ///
        ///
        ///
        public static List FindSubDirectories(string directory, int maxCount)
        {
            List subDirectories = new List();
            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 FindSubDirectories(string directory)
        {
            return Directory.GetDirectories(directory, "*", SearchOption.AllDirectories).ToList();
        }
        ///
        /// 根據時間查詢子目錄
        ///

        ///
        ///
        ///
        public static List FindSubDirectories(string directory, int maxCount, int days)
        {
            List subDirectories = new List();
            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;
        }
        ///
        /// 將原目錄移動到目標目錄
        ///

        ///
        ///
        ///
        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;
        }
        ///
        /// 重新生成新的檔案路徑
        ///

        ///
        ///
        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;
        }
        ///
        /// 獲取指定目錄下的所有檔案和資料夾大小
        ///

        /// 目錄路徑
        /// string,返回所有資料夾名字
        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;
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-773370/,如需轉載,請註明出處,否則將追究法律責任。

相關文章