iis开启的目录浏览功能默认是按照文件名称排序的,但我们有个特殊的要求,想要按照时间排序,搜索了下,没有相关方法,于是有了本篇内容 <%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <!DOCTYPE html> <% var baseDir = HttpContext.Current.Server.MapPath("~/"); var currentDir = HttpContext.Current.Request.QueryString["dir"]; currentDir = string.IsNullOrWhiteSpace(currentDir) ? baseDir : Path.Combine(baseDir, currentDir); if (!currentDir.EndsWith("\\")) currentDir += "\\"; var baseDirInfo = new DirectoryInfo(baseDir); var currentDirInfo = new DirectoryInfo(currentDir); var myDirs = Directory.GetDirectories(currentDir); var tmpDirInfos = (from d in myDirs select new DirectoryInfo(d)).ToArray(); var myDirInfos = tmpDirInfos .Where(a => a.Name.StartsWith("★")) .OrderBy(a => a.CreationTime) .ToArray() .Concat(tmpDirInfos .Where(a => !a.Name.StartsWith("★")) .OrderBy(a => a.CreationTime) .ToArray()) .ToArray(); var myFiles = Directory.GetFiles(currentDir); var myFileInfos = (from f in myFiles select new FileInfo(f)).OrderBy(a => a.LastWriteTime).ToList(); %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> body { font-size: 12px } table { width: 100%; border: 1px solid #ddd; border-spacing: 0; border-radius: 5px; border-bottom: none; } td, th { border-bottom: 1px solid #ddd; padding: 5px; text-align: left; } </style> </head> <body> <% if (currentDir.StartsWith(baseDir)) { %> <h2> <%= HttpContext.Current.Request.Url.Host %><br /> </h2> <hr /> <div style="padding: 5px">带 ★ 的文件夹将排在前面</div> <table> <thead> <tr> <th style="width: 150px">时间</th> <th>名称</th> <th>大小</th> </tr> </thead> <tbody> <% if (currentDirInfo.FullName.Length > baseDirInfo.FullName.Length) { if (currentDirInfo.Parent != null) { var parentDirStr = currentDirInfo.Parent.FullName; if (!parentDirStr.EndsWith("\\")) parentDirStr += "\\"; %> <tr> <td>- </td> <td> <a href="?dir=<%=parentDirStr.Replace(baseDir, "") %>">../</a> </td> <td>- </td> </tr> <% } }%> <%foreach (var dirInfo in myDirInfos) { %> <tr> <td> <%= dirInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") %> </td> <td> <a href="?dir=<%=dirInfo.FullName.Replace(baseDir,"") %>"><%= dirInfo.Name %></a> </td> <td>- </td> </tr> <%}%> <%foreach (var fileInfo in myFileInfos) { if(fileInfo.FullName.Equals(Path.Combine(baseDir,"web.config"),StringComparison.OrdinalIgnoreCase))continue; if(fileInfo.FullName.Equals(Path.Combine(baseDir,"Default.aspx"),StringComparison.OrdinalIgnoreCase))continue; if(fileInfo.FullName.Equals(Path.Combine(baseDir,"Web.Debug.config"),StringComparison.OrdinalIgnoreCase))continue; %> <% %> <tr> <td> <%= fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") %> </td> <td> <a href="<%=fileInfo.FullName.Replace(baseDir,"").Replace("\\","/") %>"> <%= fileInfo.Name %> </a> </td> <td> <%= fileInfo.Length %> </td> </tr> <%}%> </tbody> </table> <% } else { %> <div>越界</div> <% } %> </body> </html> 将上面内容拷贝写入 Default.aspx文件,然后将他放到开启目录浏览的根目录下,然后在浏览相关目录,将得到按照时间升序排列。 参考资料:https://blog.csdn.net/cxwl3sxl/article/details/128120902
|