C # Implementation sorts the files in the specified folder by modified time

Source: Internet
Author: User

String path = "~/document/introduction/team management System/";
DirectoryInfo dirinfo = new DirectoryInfo (Server.MapPath (path));
fileinfo[] Files = Dirinfo. GetFiles ();
Array.sort<fileinfo> (Files, New Filelasttimecomparer ());

Show download links for the two most recently updated files on a page
for (int i = 0; i < 2; i++)
{
Literal Literal = new Literal ();
Literal. Text = "</br>";
PANEL1.CONTROLS.ADD (literal);//Implement line break display
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);//Decrement
Return X.lastwritetime.compareto (y.lastwritetime);//Increment
}
}

Using System.IO;

 private void Button1_Click (object sender, EventArgs e) {//directoryinfo[] list = new DirectoryInfo (path). GetDirectories ();//Path: Folder path directoryinfo[] List = new DirectoryInfo (@ "D:\ software"). GetDirectories (); Array.sort<directoryinfo> (list, new Directorylasttimecomparer ());  textbox1.text = List[list. LENGTH-1]. The full path of the fullname;//folder and the name TextBox1.Text = list[list. LENGTH-1]. Name; The name of the folder} private void Button2_Click (object sender, EventArgs e) {fileinfo[] list = new DirectoryInfo (@ "D:\ software"). GetFiles (); Array.sort<fileinfo> (list, new Filelasttimecomparer ());  textbox1.text = List[list. LENGTH-1]. The full path of the fullname;//file and the name TextBox1.Text = list[list. LENGTH-1]. Name; The name of the file in the  } } //folder is sorted by time the most recent file read public class Directorylasttimecomparer:icomparer<directoryinfo >{#region icomparer<directoryinfo> member  public int Compare (DirectoryInfo x, DirectoryInfo y) {return x. Lastwritetime.compareto (y.lastwritetime);//Sort by name//return X.fullname.compareto (y.fullname);//Increment//return Y.fullname.compareto (x.fullname);//decrement  //Sort by Modified date//return X.lastwritetime.compareto (Y. LastWriteTime);//Increment//return Y.lastwritetime.compareto (x.lastwritetime);//decrement}  #endregion} // The folder is sorted by time the most recent file read public class filelasttimecomparer:icomparer<fileinfo>{#region icomparer<fileinfo> member  public int Compare (FileInfo x, FileInfo y) {return x.lastwritetime.compareto (y.lastwritetime);}   #endregion}

You can use the Array.Sort () method to invoke it. Of course, you can also sort by creation time.

[CSharp]View Plaincopyprint?
  1. <pre></pre>
  2. <pre></pre>
  3. <pre></pre>
  4. <pre></pre>
  5. <pre></pre>
  6. <pre></pre>
  7. <pre></pre>
  8. <pre></pre>
  9. <pre></pre>
  10. <pre></pre>
  11. <pre></pre>
  12. <pre></pre>
  13. <pre></pre>
  14. <pre></pre>
  15. <pre></pre>
  16. <div style="padding-top:20px" >
  17. <p style="FONT-SIZE:12PX;" > Copyright: This article is for bloggers original articles, not allowed to be reproduced without the permission of Bo master. </p>
  18. </div>

C # Implementation sorts the files in the specified folder by modified time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.