[Implement] 取得 Local 指定目錄下的所有檔案與目錄

[Implement] 取得 Local 指定目錄下的所有檔案與目錄

{
    static void Main(string[] args)
    {
        Program p = new Program();

        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"d:\test\");
        List<ObjectInfo> result = p.GetDirectoryList(di, "*");

        List<ObjectInfo> fileList = new List<ObjectInfo>();
        p.GetAllFiles(di, "*", ref fileList);

    }


    /// <summary>
    /// 取得指定目錄下的所有檔案與目錄(不包含子目錄下的檔案與目錄)
    /// </summary>
    /// <param name="directoryInfo"></param>
    /// <param name="searchPattern"></param>
    /// <returns></returns>
    private List<ObjectInfo> GetDirectoryList(System.IO.DirectoryInfo directoryInfo, string searchPattern)
    {
        List<ObjectInfo> objInfoList = new List<ObjectInfo>();

        //get files
        foreach (System.IO.FileInfo fi in directoryInfo.GetFiles(searchPattern))
        {
            objInfoList.Add(new ObjectInfo(fi.Name, fi.FullName, fi.Extension, fi.CreationTime, true, false));
        }

        //get directories
        foreach (System.IO.DirectoryInfo di in directoryInfo.GetDirectories(searchPattern))
        {
            objInfoList.Add(new ObjectInfo(di.Name, di.FullName, di.Extension, di.CreationTime, false, true));
        }

        return objInfoList;
    }

    /// <summary>
    /// 取得指定目錄下的所有檔案(包含子目錄下的檔案)
    /// </summary>
    /// <param name="di">DirectoryInfo</param>
    /// <param name="searchPattern">Search Pattern</param>
    /// <param name="fileList">搜尋結果清單</param>
    /// <returns></returns>
    private List<ObjectInfo> GetAllFiles(System.IO.DirectoryInfo di, string searchPattern, ref List<ObjectInfo> fileList)
    {
        //List<ObjectInfo> fileList = new List<ObjectInfo>();

        //get files
        foreach (System.IO.FileInfo fi in di.GetFiles(searchPattern))
        {
            fileList.Add(new ObjectInfo(fi.Name, fi.FullName, fi.Extension, fi.CreationTime, true, false));
        }

        // Search in subdirctories
        foreach (System.IO.DirectoryInfo d in di.GetDirectories())
        {
            GetAllFiles(d, searchPattern, ref fileList); //遞迴方法
        }

        return fileList;
    }

    /// <summary>
    /// 物件資訊
    /// </summary>
    private class ObjectInfo
    {
        private string objectName;
        private string objectFullName;
        private string objectExtension;
        private DateTime objectCreationTime;
        private bool objectIsFile;
        private bool objectIsDirectory;

        /// <param name="name">物件名稱</param>
        /// <param name="fullName">物件完整路徑</param>
        /// <param name="rootName">物件根目錄</param>
        /// <param name="extension">物件副檔名</param>
        /// <param name="creationTime">物件建立時間</param>
        /// <param name="isFile">是否為檔案</param>
        /// <param name="isDirectory">是否為資料夾</param>
        public ObjectInfo(string name, string fullName, string extension,
            DateTime creationTime, bool isFile, bool isDirectory)
        {
            objectName = name;
            objectFullName = fullName;
            objectExtension = extension;
            objectCreationTime = creationTime;
            objectIsFile = isFile;
            objectIsDirectory = isDirectory;
        }
        /// <summary>
        /// 取得物件名稱
        /// </summary>
        /// <returns>string</returns>
        public string GetName()
        {
            return objectName;
        }
        /// <summary>
        /// 取得物件完整路徑
        /// </summary>
        /// <returns>string</returns>
        public string GetFullName()
        {
            return objectFullName;
        }
        /// <summary>
        /// 取得物件副檔名
        /// </summary>
        /// <returns>string</returns>
        public string GetExtension()
        {
            return objectExtension;
        }
        /// <summary>
        /// 取得物件建立時間
        /// </summary>
        /// <returns>DateTime</returns>
        public DateTime GetCreationTime()
        {
            return objectCreationTime;
        }
        /// <summary>
        /// 物件是否為檔案
        /// </summary>
        /// <returns>bool</returns>
        public bool IsFile()
        {
            return objectIsFile;
        }
        /// <summary>
        /// 物件是否為資料夾
        /// </summary>
        /// <returns>bool</returns>
        public bool IsDirectory()
        {
            return objectIsDirectory;
        }
    }
}

也可使用 System.IO.FileAttributes 取得檔案資訊 : http://msdn.microsoft.com/zh-tw/library/system.io.fileattributes.aspx