string path = "~/Document/Introduction/团队管理制度/";
DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath(path)); FileInfo[] Files = dirinfo.GetFiles(); Array.Sort<FileInfo>(Files, new FIleLastTimeComparer());//在页面上显示最近更新的两个文件的下载链接 for (int i = 0; i < 2; i++) { Literal literal = new Literal(); literal.Text = "</br>"; Panel1.Controls.Add(literal);//实现换行显示 HyperLink hypelink = new HyperLink(); hypelink.NavigateUrl = path + Files[i].Name; hypelink.Text = Files[i].Name; Panel1.Controls.Add(hypelink);}
public class FIleLastTimeComparer : IComparer<FileInfo>
{ public int Compare(FileInfo x, FileInfo y) { return y.LastWriteTime.CompareTo(x.LastWriteTime);//递减 //return x.LastWriteTime.CompareTo(y.LastWriteTime);//递增 } }
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
//DirectoryInfo[] list = new DirectoryInfo(path).GetDirectories();// path:文件夹路径
DirectoryInfo[] list = new DirectoryInfo(@"D:\软件").GetDirectories();
Array.Sort<DirectoryInfo>(list, new DirectoryLastTimeComparer());
textBox1.Text = list[list.Length - 1].FullName;//文件夹的全路径以及名字
textBox1.Text = list[list.Length - 1].Name; //文件夹的名字
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo[] list = new DirectoryInfo(@"D:\软件").GetFiles();
Array.Sort<FileInfo>(list, new FIleLastTimeComparer());
textBox1.Text = list[list.Length - 1].FullName;//文件的全路径以及名字
textBox1.Text = list[list.Length - 1].Name; //文件的名字
}
}
//文件夹中按时间排序最新的文件读取
public class DirectoryLastTimeComparer : IComparer<DirectoryInfo>
{
#region IComparer<DirectoryInfo> 成员
public int Compare(DirectoryInfo x, DirectoryInfo y)
{
return x.LastWriteTime.CompareTo(y.LastWriteTime);
//依名称排序
//return x.FullName.CompareTo(y.FullName);//递增
//return y.FullName.CompareTo(x.FullName);//递减
//依修改日期排序
//return x.LastWriteTime.CompareTo(y.LastWriteTime);//递增
//return y.LastWriteTime.CompareTo(x.LastWriteTime);//递减
}
#endregion
}
//文件夹中按时间排序最新的文件读取
public class FIleLastTimeComparer : IComparer<FileInfo>
{
#region IComparer<FileInfo> 成员
public int Compare(FileInfo x, FileInfo y)
{
return x.LastWriteTime.CompareTo(y.LastWriteTime);
}
#endregion
}
就可以使用Array.Sort()方法来对其进行调用。当然,你也可以按创建时间来进行排序。