C# Directory 遍历文件夹

2021-03-23  本文已影响0人  super41
前言:编辑器拓展常常要访问到文件目录。所以文件相关的操作挺重要的。这里记录下遍历目录的方法:
  1. 这里先列举下会用到的类

    • FileInfo : 文件类(不包括目录)
    • DirectoryInfo : 目录类
    • FileSystemInfo :通用文件类,可以是文件,也可以是目录。
      可以通过FileSystemInfo.Attributes 来判断是不是目录:
      bool isDir = fileSystemInfo.Attributes == FileAttributes.Directory;
  2. 遍历目录里的所有文件

    • 获取一个目录里的所有文件
        public void AccessAllFile(string dirPath)
        {
          DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
          //SearchOption.AllDirectories 代表遍历子文件夹。
          //SearchOption.TopDirectoryOnly 或 不加参数 则只访问第一层文件
          FileInfo[] fileInfos = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
          for (int i = 0; i < fileInfos.Length; i++)
          {
              Log.Debug($"name:{fileInfos[i].Name}");
          }
        }

        public void AccessAllDir(string dirPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            DirectoryInfo[] dirInfos = directoryInfo.GetDirectories("*", SearchOption.AllDirectories);
            for (int i = 0; i < dirInfos.Length; i++)
            {
                Log.Debug($"name:{dirInfos[i].Name}");
            }
        }
        public void AccessAllFileAndDir(string dirPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos("*",SearchOption.AllDirectories);
            for (int i = 0; i < fileSystemInfos.Length; i++)
            {
                var file = fileSystemInfos[i];
                Log.Debug($"isDir:{file.Attributes == FileAttributes.Directory} name:{file.Name}");
            }
        }
        public void AccessAllFileAndDir2(string dirPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();
            for (int i = 0; i < fileSystemInfos.Length; i++)
            {
                var fileSystemInfo = fileSystemInfos[i];
                bool isDir = fileSystemInfo.Attributes == FileAttributes.Directory;
                if (isDir)
                {
                    Debug.Log($"dir name:{fileSystemInfo.Name}");
                    AccessAllFileAndDir2(fileSystemInfo.FullName);
                }
                else
                {
                    Debug.Log($"file name:{fileSystemInfo.Name}");
                }
            }
        }
        public void DeleteAllFile(string dir)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dir);
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();
            for (int i = 0; i < fileSystemInfos.Length; i++)
            {
                FileSystemInfo file = fileSystemInfos[i];
                bool isDir = file.Attributes == FileAttributes.Directory;
                if (isDir)
                {
                    Directory.Delete(file.FullName,true);
                }
                else
                {
                    File.Delete(file.FullName);
                }
            }
        }
上一篇下一篇

猜你喜欢

热点阅读