C#遍历计算机上所有的文件

class Program
    {
        static List<string> allFileName=new List<string>();
        static void Main(string[] args)
        {       
            string[] drives = Directory.GetLogicalDrives();
            foreach (string drive in drives)
            {
                getAll(drive);
            }
        }
        static void getAll(string path)
        {
            DirectoryInfo sDir = new DirectoryInfo(path);
            try
            {
                FileInfo[] fileArray = sDir.GetFiles();
                foreach (FileInfo file in fileArray)
                {
                    allFileName.Add(file.FullName);
                }
            }
            catch(Exception e)
            {
            }
 
            try
            {
                DirectoryInfo[] subDirArray = sDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirArray)
                {
                    getAll(subDir.FullName);
                }
            }
            catch (Exception e)
            {
            }
        }
    }

 

posted @ 2015-07-06 11:25  如.若  阅读(665)  评论(0编辑  收藏  举报