找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1179|回复: 0
打印 上一主题 下一主题

Asp.Net文件和文件夹操作大全

[复制链接]

2647

主题

2647

帖子

7881

积分

论坛元老

Rank: 8Rank: 8

积分
7881
跳转到指定楼层
楼主
发表于 2018-2-18 04:50:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

                  ///
        /// 创建文件夹
        ///
        ///
        public static void FolderCreate(string Path)
        {
            // 判断目标目录是否存在如果不存在则新建之
            if (!Directory.Exists(Path))
                Directory.CreateDirectory(Path);
        }
        #endregion
        #region 创建目录
        public static void FileCreate(string Path)
        {
            FileInfo CreateFile = new FileInfo(Path); //创建文件
            if (!CreateFile.Exists)
            {
                FileStream FS = CreateFile.Create();
                FS.Close();
            }
        }
        #endregion
        #region 递归删除文件夹目录及文件
        /****************************************
         * 函数名称:DeleteFolder
         * 功能说明:递归删除文件夹目录及文件
         * 参    数:dir:文件夹路径
         * 调用示列:
         *           string dir = Server.MapPath( "test/");
         *           EC.FileObj.DeleteFolder(dir);      
        *****************************************/
        ///
        /// 递归删除文件夹目录及文件
        ///
        ///  
        ///
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件                        
                    else
                        DeleteFolder(d); //递归删除子文件夹
                }
                Directory.Delete(dir, true); //删除已空文件夹                 
            }
        }
        #endregion
        #region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
        /****************************************
         * 函数名称:CopyDir
         * 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
         * 参    数:srcPath:原始路径,aimPath:目标文件夹
         * 调用示列:
         *           string srcPath = Server.MapPath( "test/");
         *           string aimPath = Server.MapPath( "test1/");
         *           EC.FileObj.CopyDir(srcPath,aimPath);   
        *****************************************/
        ///
        /// 指定文件夹下面的所有内容copy到目标文件夹下面
        ///
        /// 原始路径
        /// 目标文件夹
        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加之
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                // 判断目标目录是否存在如果不存在则新建之
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                //如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                //string[] fileList = Directory.GetFiles(srcPath);
                string[] fileList = Directory.GetFileSystemEntries(srcPath);
                //遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                        CopyDir(file, aimPath + Path.GetFileName(file));
                    //否则直接Copy文件
                    else
                        File.Copy(file, aimPath + Path.GetFileName(file), true);
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.ToString());
            }
        }
        #endregion
        #region 获取指定文件夹下所有子目录及文件(树形)
        /****************************************
         * 函数名称:GetFoldAll(string Path)
         * 功能说明:获取指定文件夹下所有子目录及文件(树形)
         * 参    数:Path:详细路径
         * 调用示列:
         *           string strDirlist = Server.MapPath( "templates");      
         *           this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);
        *****************************************/
        ///
        /// 获取指定文件夹下所有子目录及文件
        ///
        /// 详细路径
        public static string GetFoldAll(string Path)
        {
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str);
            return str;
        }
        ///
        /// 获取指定文件夹下所有子目录及文件函数
        ///
        /// 指定目录
        /// 默认起始值,调用时,一般为0
        /// 用于迭加的传入值,一般为空
        ///
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//递归目录 文件
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
            foreach (DirectoryInfo dirinfo in subDirectories)
            {
                if (nLevel == 0)
                {
                    Rn += "├";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i
            }
            return Rn;
        }
        /****************************************
         * 函数名称:GetFoldAll(string Path)
         * 功能说明:获取指定文件夹下所有子目录及文件(下拉框形)
         * 参    数:Path:详细路径
         * 调用示列:
         *            string strDirlist = Server.MapPath( "templates");      
         *            this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist, "tpl","");
        *****************************************/
        ///
        /// 获取指定文件夹下所有子目录及文件(下拉框形)
        ///
        /// 详细路径
        /// 下拉列表名称
        /// 默认选择模板名称
        public static string GetFoldAll(string Path,string DropName,string tplPath)
        {
            string strDrop = "--请选择详细模板--";
            string str = "";
            DirectoryInfo thisOne = new DirectoryInfo(Path);
            str = ListTreeShow(thisOne, 0, str,tplPath);
            return strDrop+str+ "";
        }
        ///
        /// 获取指定文件夹下所有子目录及文件函数
        ///
        /// 指定目录
        /// 默认起始值,调用时,一般为0
        /// 用于迭加的传入值,一般为空
        /// 默认选择模板名称
        ///
        public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//递归目录 文件
        {
            DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
            foreach (DirectoryInfo dirinfo in subDirectories)
            {
                Rn += "";
                if (nLevel == 0)
                {
                    Rn += "┣";
                }
                else
                {
                    string _s = "";
                    for (int i = 1; i ";
                FileInfo[] fileInfo = dirinfo.GetFiles();   //目录下的文件
                foreach (FileInfo fInfo in fileInfo)
                {
                    Rn += "";
                    if (nLevel == 0)
                    {
                        Rn += "│ ├";
                    }
                    else
                    {
                        string _f = "";
                        for (int i = 1; i ";
                }
                Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);
            }
            return Rn;
        }
        #endregion
        #region 获取文件夹大小
        /****************************************
         * 函数名称:GetDirectoryLength(string dirPath)
         * 功能说明:获取文件夹大小
         * 参    数:dirPath:文件夹详细路径
         * 调用示列:
         *           string Path = Server.MapPath( "templates");
         *           Response.Write(EC.FileObj.GetDirectoryLength(Path));      
        *****************************************/
        ///
        /// 获取文件夹大小
        ///
        /// 文件夹路径
        ///
        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
                return 0;
            long len = 0;
            DirectoryInfo di = new DirectoryInfo(dirPath);
            foreach (FileInfo fi in di.GetFiles())
            {
                len += fi.Length;
            }
            DirectoryInfo[] dis = di.GetDirectories();
            if (dis.Length > 0)
            {
                for (int i = 0; i
        #region 获取指定文件详细属性
        /****************************************
         * 函数名称:GetFileAttibe(string filePath)
         * 功能说明:获取指定文件详细属性
         * 参    数:filePath:文件详细路径
         * 调用示列:
         *           string file = Server.MapPath( "robots.txt");
         *            Response.Write(EC.FileObj.GetFileAttibe(file));         
        *****************************************/
        ///
        /// 获取指定文件详细属性
        ///
        /// 文件详细路径
        ///
        public static string GetFileAttibe(string filePath)
        {
            string str = "";
            System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
            str += "详细路径:" + objFI.FullName + "
文件名称:" + objFI.Name + "
文件长度:" + objFI.Length.ToString() + "字节
创建时间" + objFI.CreationTime.ToString() + "
最后访问时间:" + objFI.LastAccessTime.ToString() + "
修改时间:" + objFI.LastWriteTime.ToString() + "
所在目录:" + objFI.DirectoryName + "
扩展名:" + objFI.Extension;
            return str;
        }
        #endregion
    }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户反馈
客户端