获取磁盘目录与文件名

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);
            }
        }
    }
}

你可能感兴趣的:(获取磁盘目录与文件名)