当前位置:  编程技术>.net/c#/asp.net

DirectInfo.GetFiles返回数组的默认排序示例

    来源: 互联网  发布时间:2014-10-26

    本文导语:  代码如下:using System;using System.Collections;using System.IO; namespace ConsoleApplication1{    ///     /// 文件排序类    ///     public class FileSort : IComparer    {        private FileOrder _fileorder;        private FileAsc _fileasc;         //...

代码如下:

using System;
using System.Collections;
using System.IO;

namespace ConsoleApplication1
{
    ///
    /// 文件排序类
    ///
    public class FileSort : IComparer
    {
        private FileOrder _fileorder;
        private FileAsc _fileasc;

        ///
        /// 构造函数
        ///
        public FileSort()
            : this(FileOrder.Name, FileAsc.Asc)
        { }

        ///
        /// 构造函数
        ///
        ///
        public FileSort(FileOrder fileorder)
            : this(fileorder, FileAsc.Asc)
        { }

        ///
        /// 构造函数
        ///
        ///
        ///
        public FileSort(FileOrder fileorder, FileAsc fileasc)
        {
            _fileorder = fileorder;
            _fileasc = fileasc;
        }

        ///
        /// 比较函数
        ///
        ///
        ///
        ///
        public int Compare(object x, object y)
        {
            FileInfo file1 = x as FileInfo;
            FileInfo file2 = y as FileInfo;
            FileInfo file3;

            if (file1 == null || file2 == null)
                throw new ArgumentException("参数不是FileInfo类实例.");

            if (_fileasc == FileAsc.Desc)
            {
                file3 = file1;
                file1 = file2;
                file2 = file3;
            }

            switch (_fileorder)
            {
                case FileOrder.Name:
                    return file1.Name.CompareTo(file2.Name);
                case FileOrder.Length:
                    return file1.Length.CompareTo(file2.Length);
                case FileOrder.Extension:
                    return file1.Extension.CompareTo(file2.Extension);
                case FileOrder.CreationTime:
                    return file1.CreationTime.CompareTo(file2.CreationTime);
                case FileOrder.LastAccessTime:
                    return file1.LastAccessTime.CompareTo(file2.LastAccessTime);
                case FileOrder.LastWriteTime:
                    return file1.LastWriteTime.CompareTo(file2.LastWriteTime);
                default:
                    return 0;
            }
        }
    }

    ///
    /// 排序依据
    ///
    public enum FileOrder
    {
        ///
        /// 文件名
        ///
        Name,
        ///
        /// 大小
        ///
        Length,
        ///
        /// 类型
        ///
        Extension,
        ///
        /// 创建时间
        ///
        CreationTime,
        ///
        /// 访问时间
        ///
        LastAccessTime,
        ///
        /// 修改时间
        ///
        LastWriteTime
    }

    ///
    /// 升序降序
    ///
    public enum FileAsc
    {
        ///
        /// 升序
        ///
        Asc,
        ///
        /// 降序
        ///
        Desc
    }
}


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3