ASP.NET利用GetFiles取得目錄檔案清單,結合Array.Sort來排序檔案列表順序

using System;
02using System.Data;
03using System.Configuration;
04using System.Collections;
05using System.Web;
06using System.Web.Security;
07using System.Web.UI;
08using System.Web.UI.WebControls;
09using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12 
13public partial class SortFileList : System.Web.UI.Page
14{
15    protected void Page_Load(object sender, EventArgs e)
16    {
17        DirectoryInfo dirinfo = new DirectoryInfo(MapPath("~"));
18        FileInfo[] sortList = dirinfo.GetFiles();
19        Array.Sort(sortList, new MyDateSorter());
20 
21        foreach (FileInfo item in sortList)
22        {
23            Response.Write(item.FullName + "<br" + " />");
24        }
25    }
26}
27 
28public class MyDateSorter : IComparer
29{
30    #region IComparer Members
31    public int Compare(object x, object y)
32    {
33        if (x == null && y == null)
34        {
35            return 0;
36        }
37        if (x == null)
38        {
39            return -1;
40        }
41        if (y == null)
42        {
43            return 1;
44        }
45        FileInfo xInfo = (FileInfo)x;
46        FileInfo yInfo = (FileInfo)y;
47 
48 
49        //依名稱排序
50        return xInfo.FullName.CompareTo(yInfo.FullName);//遞增
51        //return yInfo.FullName.CompareTo(xInfo.FullName);//遞減
52 
53        //依修改日期排序
54        //return xInfo.LastWriteTime.CompareTo(yInfo.LastWriteTime);//遞增
55        //return yInfo.LastWriteTime.CompareTo(xInfo.LastWriteTime);//遞減
56    }
57    #endregion
58}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值