zoukankan      html  css  js  c++  java
  • C#利用DirectoryInfo遍历文件夹所有文件的类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;//
    using System.Collections;//
    
    namespace ControlFile
    {
        /// <summary>
        /// 文件操作类
        /// </summary>
        public class ControlFileClass
        {
             //字段声明
            private ArrayList fileListPath = new ArrayList();
            private ArrayList fileListName = new ArrayList();
    
            /// <summary>
            /// 文件路径
            /// </summary>
            public ArrayList FileListPath
            {
                get { return fileListPath; }
            }
    
            /// <summary>
            /// 文件名称
            /// </summary>
            public ArrayList FileListName
            {
                get { return fileListName; }
            }
    
            /// <summary>
            /// 构造函数并遍历文件夹获取文件名称,路径
            /// </summary>
            /// <param name="sourceDirectory">文件夹路径</param>
            /// <param name="nextFold">是否继续查找更深路径</param>
            public ControlFileClass(string sourceDirectory, bool nextFold)
            {
                DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
                GetAllList(diSource,nextFold);
            }
    
            public void GetAllList(DirectoryInfo source, bool nextFold)
            {
                try
                {
                    foreach (FileInfo fi in source.GetFiles())
                    {
                        fileListPath.Add(fi.FullName);
                        fileListName.Add(fi.Name);
                    }
                    if (nextFold)//如果设置为可以向更深目录遍历则遍历
                    {
                        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
                        {
                            GetAllList(diSourceSubDir, nextFold);
                        }
                    }
                    else//如果设置为不向更深目录遍历则直接用文件夹表示
                    {
                        //遍历获取文件夹
                        foreach (DirectoryInfo d in source.GetDirectories())
                        {
                            fileListPath.Add(d.FullName);
                            fileListName.Add(d.Name);
                        }
                    }
                }
                catch
                { }
            }
    
            /// <summary>
            /// 判断一个路径是文件还是文件夹
            /// </summary>
            /// <param name="fileName">文件路径</param>
            /// <returns>true:文件夹,false:文件</returns>
            public static bool IsFolder(string fileNamePath)
            {
                FileInfo fileInfo = new FileInfo(fileNamePath);
                if ((fileInfo.Attributes & FileAttributes.Directory) != 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 删除指定文件夹
            /// </summary>
            /// <param name="dir">文件夹路径</param>
            public static void DeleteFolder(string dirPath)
            {
                foreach (string d in Directory.GetFileSystemEntries(dirPath))
                {
                    if (File.Exists(d))
                    {
                        FileInfo fi = new FileInfo(d);
                        if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            fi.Attributes = FileAttributes.Normal;
                        File.Delete(d);//直接删除其中的文件   
                    }
                    else
                    {
                        DeleteFolder(d);//递归删除子文件夹   
                    }
                }
                Directory.Delete(dirPath);//删除已空文件夹   
            }
        }
    }
  • 相关阅读:
    实用技巧——让你的网站变成响应式的3个简单步骤
    C#网络编程初步之TCP
    C#调用WebService实例和开发
    获取GridView中RowCommand的当前索引行
    创建Spring项目 IOException parsing XML document from class path resource [ApplicationContext.xml];
    java8的日期处理
    使用IDEA创建动态web项目
    连接数据库出现The server time zone value '�й���׼ʱ��' is unrecogni的解决方案
    Spring Boot RabbitMQ 应用场景--转载
    SpringBoot可视化监控
  • 原文地址:https://www.cnblogs.com/mane/p/1917961.html
Copyright © 2011-2022 走看看