C#遍历所有磁盘文件,并输出文件的绝对路径

Posted 富的只剩下代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#遍历所有磁盘文件,并输出文件的绝对路径相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 查找特定目录下的子目录

    class Program
   
        static int filesSum=0;
        static void Main(string[] args)
       
           
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo d in allDrives)
           
                DirectoryInfo dir = new DirectoryInfo(d.Name );
                getFilesPath(dir);
          
            Console.WriteLine("文件总数:0", filesSum );
            Console.ReadLine();
       
        private static void getFilesPath(DirectoryInfo dir)
       
            try
           
                FileInfo[] files = dir.GetFiles();
                foreach (FileInfo fil in files)
               
                    filesSum++;
                    Console.WriteLine(fil.FullName);
               
           
            catch (System.StackOverflowException e)
           
                Console.WriteLine(e.Message);
           
            catch (System.UnauthorizedAccessException su)
           
                Console.WriteLine(su.Message);
           
            catch (IOException IOe)
           
                Console.WriteLine(IOe.Message );
           
            try
           
                DirectoryInfo[] dirs = dir.GetDirectories();
                foreach (DirectoryInfo dir1 in dirs)
               
                    getFilesPath(dir1);
               
               
           
            catch (System.StackOverflowException e)
           
                Console.WriteLine(e.Message );
           
            catch (System.UnauthorizedAccessException su)
           
                Console.WriteLine(su.Message);
           
            catch (IOException IOe)
           
                Console.WriteLine(IOe.Message);
           
       
   

以上是关于C#遍历所有磁盘文件,并输出文件的绝对路径的主要内容,如果未能解决你的问题,请参考以下文章

案例:遍历目录

交互式shell脚本遍历文件目录下的所有文件和目录(绝对路径)

交互式shell脚本遍历文件目录下的所有文件和目录(绝对路径)

用java语言统计任一磁盘下目录下文件夹总数,文件总数?

C# 计算输入和输出 FileStream 的 MD5

java代码实现输出指定以.java结尾的文件的绝对路径