日期:2014-05-18 浏览次数:20913 次
string[] filePath = { "zswang.txt", "csdn.exe", "a.1" }; //绝对文件全路径 ArrayList _fileSuffix = new ArrayList(); _fileSuffix.Add("jpg"); // 后缀列表 _fileSuffix.Add("1"); // 后缀列表 bool exists = false; for (int j = 0; j < filePath.Length; j++) { for (int i = 0; i < _fileSuffix.Count; i++) { if (string.Compare(Path.GetExtension(filePath[j]), "." + _fileSuffix[i].ToString(), true) == 0) { exists = true; break; } } } Console.WriteLine(exists);
------解决方案--------------------
看你的写法也像C# 3.0吧,那干脆用Linq吧。。。。
string[] filePath = { "txt", "exe", "jpg" };
List<string> _fileSuffix = new List<string> { "jpg", "jpeg", "jpe", "jfif", "bmp", "png", "tif", "tiff", "gif" }; ; // 后缀列表
bool b = (from t1 in filePath where _fileSuffix.Contains(t1) select t1).Count() > 0;
------解决方案--------------------
不需要用两次循环,一次就可以了。
string[] filePath;
ArrayList _fileSuffix = new ArrayList();
public bool check()
{
bool b = false;
for (int i = 0; i < filePath.Length; i++)
{
if (_fileSuffix.IndexOf(filePath[i]) > -1)
{
b = true;
break;
}
}
return b;
}
------解决方案--------------------
用ArrayList..::.IndexOf 方法就行啊.
foreach (string s in filePath)
if(_fileSuffix.IndexOf(s)>=0 )
return true;
return false;