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?
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <div style="padding-top:20px" >
- <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>
- </div>
C # Implementation sorts the files in the specified folder by modified time