string[] files1 = Directory.GetFiles(sSourceFolder, "*.txt", SearchOption.AllDirectories);// 表示 搜索 对应的目录及其所有的子目录
string[] files2 = Directory.GetFiles(sSourceFolder, "*.csv", SearchOption.AllDirectories);
// 如果只搜索对应目录 可用 Directory.GetFiles(sSourceFolder, "*.csv");
List<string> lstFiles = new List<string>();
lstFiles.AddRange(files1);
lstFiles.AddRange(files2);
取多种类型的文件
public static string[] GetFiles(string sourceFolder, string filters, SearchOption searchOption)
{
return filters.Split('|').SelectMany(filter => Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray();
}
public static List<string> GetLFiles(string sourceFolder, string filters, SearchOption searchOption)
{
return filters.Split('|').SelectMany(filter => Directory.GetFiles(sourceFolder, filter, searchOption)).ToList();
}
调用样例:
string sFolder=@"your files folder";
string[] arrTmp = Common.GetFiles(sFolder, "FileA_837*.x12|FileB_837*.x12|Remove_837*.x12", SearchOption.TopDirectoryOnly);
List<string> lstTmp = Common.GetLFiles(sFolder, "FileA_837*.x12|FileB_837*.x12|Remove_837*.x12", SearchOption.TopDirectoryOnly);
// arrTmp 与 lstTmp 2个集合中的内容是一样的。
2个List取差集
//demo1:
List<string> lstA = new List<string> { "abc", "aac", "adc", "bbcc" };
List<string> lstB = new List<string> { "abc", "bbcc" };
IEnumerable<string> result1 = lstA.Except<string>(lstA.Intersect<string>(lstB));
List<string> lstC = new List<string>(result1); // get aac, adc
// 以下demo2 和 demo3中用到的 方法 ExceptWith:
static List<T> ExceptWith<T>(List<T> lstFrom, List<T> lstExcept) where T : IComparable
{
List<T> lstResult = new List<T>();
int iFromIndex, iExceptIndex, iCopyIndex;
iFromIndex = iExceptIndex = iCopyIndex = 0;
lstFrom.Sort();
lstExcept.Sort();
while ((iFromIndex < lstFrom.Count) && (iExceptIndex < lstExcept.Count))
{
int compar = ((IComparable)(lstFrom[iFromIndex].ToString().ToUpper()))
.CompareTo(lstExcept[iExceptIndex].ToString().ToUpper());
if (compar < 0)
{
iFromIndex++;
}
else if (compar > 0)
{
iExceptIndex++;
}
else if (compar == 0)
{
if (iFromIndex > iCopyIndex)
CopyData(lstFrom, lstResult, iCopyIndex, iFromIndex);
iFromIndex++;
iCopyIndex = iFromIndex;
}
}
CopyData(lstFrom, lstResult, iCopyIndex, lstFrom.Count);
return lstResult;
}
static void CopyData<T>(List<T> from, List<T> to, int startindex, int endindex)
{
if (from == null || to == null || startindex < 0 || endindex < 0 || endindex <= startindex) return;
to.AddRange(from.Where((data, index) => index >= startindex && index < endindex));
}
//demo2:
List<string> lstA = new List<string> { "abc", "aac", "adc", "bbcc", "ddef", "bbcde" };
List<string> lstB = new List<string> { "abc", "bbcc", "DDEf", "bbcdf" };
List<string> lstC = lstA.Except<string>(lstA.Intersect<string>(lstB)).ToList(); // get aac,adc,ddef,bbcde
lstC = ExceptWith(lstA,lstB).ToList(); // get aac, adc, bbcde
//demo3:
List<double> lstA = new List<double> { 1.1, 4.4, 2.2, 1.5, 6.9, 9.57, 6.9, 5.93, 5.930, 8.3, 4.120 };
List<double> lstB = new List<double> { 2.2, 1.1, 1.5, 8.668, 8.30, 4.12 };
List<double> lstC = lstA.Except<double>(lstA.Intersect<double>(lstB)).ToList(); // get 4.4, 6.9, 9.57, 5.93
lstC = ExceptWith(lstA, lstB).ToList(); // get 4.4, 5.93, 5.93, 6.9, 6.9, 9.57
数组使用contains
http://blog.sina.com.cn/s/blog_4c142e330100fr0c.html
using System.Collections;
string[] strArr = {"a","b","c","d","e"};
bool exists = ((IList)strArr).Contains("a");
http://www.cnblogs.com/greatverve/archive/2012/03/29/csharp-list-linq-Intersection.html
http://blog.youkuaiyun.com/gulingeagle/article/details/8598016
http://www.cnblogs.com/wenjian/archive/2009/08/06/1540112.html