日期:2014-05-18 浏览次数:20865 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str = @"E:\"; IList<FileInfo> lst = GetFiles(str); foreach (var item in lst) { Console.WriteLine(item); } Console.ReadKey(); } private static void GetFilePath(string strPath, ref List<string> al) { DirectoryInfo diFliles = new DirectoryInfo(strPath); DirectoryInfo[] diArr = diFliles.GetDirectories(); foreach (DirectoryInfo di in diArr) { al.Add(di.FullName); GetFilePath(di.FullName, ref al); } } /// <summary> /// 遍历当前目录及子目录 /// </summary> /// <param name="strPath">文件路径</param> /// <returns>所有文件</returns> private static IList<FileInfo> GetFiles(string strPath) { List<FileInfo> lst = new List<FileInfo>(); List<string> al = new List<string>(); try { al.Add(strPath); DirectoryInfo diFliles = null; GetFilePath(strPath,ref al); foreach (string str in al) { diFliles = new DirectoryInfo(str); lst.AddRange(diFliles.GetFiles()); } } catch { //如果设为E:\aa则不会出问题 } return lst; } } }