C#文件操作

来源:百度文库 编辑:神马文学网 时间:2024/04/28 22:48:15

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.IO;
using System.Web;

namespace PManager
{
    ///


    /// 类的作用:主要要用来对指定的目录进行遍历
    /// 编写时间:2008-08-19
    ///

    public class GetAllDirectory
    {
        ///
        /// 本方法主要用来遍历指定目录下的所有的子目录
        ///

        /// 要传入的绝对路径
        /// 返回传入路径下的所有目录文件
        public StringCollection GetDirectory(string StrRootDirectory)
        {
            StringCollection AllDirectory = new StringCollection();
            string[] AllDir = Directory.GetDirectories(StrRootDirectory);
            if (!Directory.Exists(StrRootDirectory))
                Directory.CreateDirectory(StrRootDirectory);
            if (AllDir != null)
            {
                for (int i = 0; i < AllDir.Length; i++)
                {
                    AllDirectory.Add(AllDir[i]);
                }
            }
            return AllDirectory;
        }
        public StringCollection GetDirectoryName(string StrRootDirectory)
        {
            StringCollection AllDirectoryNames = new StringCollection();
            if (!Directory.Exists(StrRootDirectory))
                Directory.CreateDirectory(StrRootDirectory);
            string[] AllDir = Directory.GetDirectories(StrRootDirectory);
            if (AllDir != null)
            {
                for (int i = 0; i < AllDir.Length; i++)
                {
                    AllDirectoryNames.Add(Path.GetFileName(AllDir[i]));
                }
            }
            return AllDirectoryNames;
        }
        ///
        /// 本方法主要用来遍历指定目录下的所有的文件
        ///

        /// 要传入的绝对路径
        /// 返回传入路径下的所有文件名称
        public StringCollection GetAllFiles(string StrParentDir)
        {
            StringCollection AllFileDir = new StringCollection();
            string[] AllFiles = Directory.GetFiles(StrParentDir);
            if (AllFiles != null)
            {
                for (int j = 0; j < AllFiles.Length; j++)
                {
                    AllFileDir.Add(AllFiles[j]);
                }
            }
            return AllFileDir;
        }
        public StringCollection GetAllFilesName(string StrParentDir)
        {
            StringCollection AllFileNameDir = new StringCollection();
            string[] AllFiles = Directory.GetFiles(StrParentDir);
            if (AllFiles != null)
            {
                for (int j = 0; j < AllFiles.Length; j++)
                {
                    AllFileNameDir.Add(Path.GetFileName(AllFiles[j]));
                }
            }
            return AllFileNameDir;
        }
        ///
        /// 本参数用来存储要输出的所有子目录
        ///

        private   StringCollection StrRTDirPath = new StringCollection();
        ///
        /// 本方法用来得到指定目录下的所有子目录
        ///

        /// 要传入的指定目录的绝对地址
        /// 指定目录下的所有子目录
        public StringCollection GetParentChildDir(string StrParentPath)
        {
            StringCollection StrDirRT = new StringCollection();
            StrDirRT = GetDirectory(StrParentPath);
            StringEnumerator StrEnum = StrDirRT.GetEnumerator();
            while (StrEnum.MoveNext())
            {
                StrRTDirPath.Add(StrEnum.Current.ToString());
                GetParentChildDir(StrEnum.Current.ToString());
            }
            return StrRTDirPath;
        }
        ///
        /// 本参数用来存储本指定目录及子目录下的所有文件的
        ///

        private StringCollection StrRTFilePath = new StringCollection();
        ///
        /// 本方法用来返回指定的所有的目录下的的所有文件
        ///

        /// 指定的目录的绝对路径
        /// 返回所有目录下的所有文件路径
        public StringCollection GetParnetChildFile(StringCollection StrDirPath)
        {
           StringCollection StrRTAllFilePath=new StringCollection();
           if (StrDirPath == null || StrDirPath.Count <= 0)
           {
               return StrRTAllFilePath;
           }
           else
           {
               StringEnumerator DirPathEnum = StrDirPath.GetEnumerator();
               while (DirPathEnum.MoveNext())
               {
                   StringCollection OnePathFile = new StringCollection();
                   OnePathFile = GetAllFiles(DirPathEnum.Current.ToString());
                   StringEnumerator OnePath = OnePathFile.GetEnumerator();
                   while (OnePath.MoveNext())
                   {
                       StrRTAllFilePath.Add(OnePath.Current.ToString());
                   }
               }
           }
           return StrRTAllFilePath;
        }
        ///
        /// 本函数用来返回指定文件的创建日期
        ///

        /// 文件的绝对路径
        /// 返回文件的创建时间
        public DateTime GetFileCreatTime(string StrFilePath)
        {
            return File.GetCreationTime(StrFilePath);
        }
        ///
        /// 本函数用来返回指定目录的创建日期
        ///

        /// 目录的绝对路径
        /// 返回目录的创建时间
        public DateTime GetDirectoryTime(string StrDirPath)
        {
            return Directory.GetCreationTime(StrDirPath);
        }
        ///
        /// 本函数用来进入到父级目录的
        ///

        /// 当前目录的路径
        /// 返回父路径
        public string GoToParent(string StrDirPath)
        {
            return Directory.GetParent(StrDirPath).FullName.ToString();
        }
    }
}