文件夹中文件夹(文件)按时间排序,读取最新的文件夹(文件)

原文:http://blog.csdn.net/xue1234567890/archive/2009/08/18/4457363.aspx

C#

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);   

  

        }  

 

        #endregion   

    }   

  

    //文件夹中按时间排序最新的文件读取   

    public class FIleLastTimeComparer : IComparer<FileInfo>   

    {  

        #region IComparer<FileInfo> 成员   

  

        public int Compare(FileInfo x, FileInfo y)   

        {   

            return x.LastWriteTime.CompareTo(y.LastWriteTime);   

        }  

 

        #endregion   

    }   

你可能感兴趣的:(文件夹)