手机版 | 登录 | 注册 | 留言 | 设首页 | 加收藏
联系客服
当前位置: 网站首页 > 程序技巧 > 文章 当前位置: 程序技巧 > 文章

iis开启的目录浏览功能默认是按照文件名称排序的,怎样要求按时间排序

时间:2023-04-20    点击: 次    来源:网络    添加者:佚名 - 小 + 大

 

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

分享按钮

上一篇:Web安全漏洞之目录遍历

下一篇:网页出现滚动条——网页的宽度超过了屏幕宽度

豫ICP备19032584号-1  |   QQ:80571569  |  地址:河南濮阳市  |  电话:13030322310  |  
Copyright © 2024 FE内容付费系统 版权所有,授权www.xingwp.cn使用 Powered by 66FE.COM