//取“Dir1”的路径
string dir1 = Server.MapPath("~/App_Data/Dir1/");
//取“Dir1”中的文件信息
List<FileInfo> files1 = new List<FileInfo>();
foreach (string file in Directory.GetFiles(dir1))
{
files1.Add(new FileInfo(file));
}
//取“Dir2”的路径
string dir2 = Server.MapPath("~/App_Data/Dir2/");
//取“Dir2”中的文件信息
List<FileInfo> files2 = new List<FileInfo>();
foreach (string file in Directory.GetFiles(dir2))
{
files2.Add(new FileInfo(file));
}
//LINQ查找出两个文件夹中包含的同名文件
var query = from file1 in files1
join file2 in files2 on file1.Name equals file2.Name
orderby file1.Name
select file1;
//显示同名文件序列
foreach (var item in query)
{
Response.Write(string.Format("{0} {1}<br/>", item.Name, item.Length));
}