Directory.GetFiles()获取多个类型格式的文件
第一种(用通配符)
1 string[] fileNameX = Directory.GetFiles(@"D:\Sjdc", "*.xls?"); 2 Array array = Array.CreateInstance(typeof(string),fileNameX.Length); 3 fileNameX.CopyTo(array, 0);
第二种
1 string[] fileNameX = Directory.GetFiles(@"D:\Sjdc", "*.*"); 2 List<string> lists = new List<string>(); 3 for (int i = 0; i < fileNameX.Length; i++) 4 { 5 string fileNamei = fileNameX[i].ToLower(); 6 if (fileNamei.EndsWith(".xls") || fileNamei.EndsWith(".xlsx")) 7 { 8 lists.Add(fileNamei); 9 } 10 } 11 Array array = Array.CreateInstance(typeof(string), lists.Count); 12 lists.CopyTo((string[])array, 0);
第三种
1 var files = Directory.GetFiles(@"D:\Sjdc", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".XLSX") || s.EndsWith(".XLS")); 2 List<string> listFile = new List<string>(); 3 foreach (string s in files) 4 { 5 listFile.Add(s); 6 }