通过ASP.NET分析IIS文档根,并获取文件的上次修改日期



我正在使用ASP.NET

我的任务是循环浏览Document Root C:\inetpub\wwwroot的所有文件夹和文件查找在过去7天内修改过的所有.aspx文件=>从这些文件中获取"title"节点的值=>"title"one_answers"last modified date"的输出值。

任何关于代码示例的指导都将不胜感激。

谢谢海克。

以下是我为上述问题编写的解决方案:

using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.Globalization;
using System.IO;
using System.Net;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public partial class DirectoryIterator : System.Web.UI.Page 
{

    public void GenerateLinks(string webpath, int linkCount)
    {
       DateTime now1 = DateTime.Now;
        string webpathnoslash = webpath.Substring(1);
        string path = Server.MapPath(webpathnoslash);
        try 
        {        
            if(Directory.Exists(path)) 
               {            
             //string[] dirs1 = Directory.GetFiles(path, "*.aspx", SearchOption.AllDirectories);
             var dirs2 = new DirectoryInfo(path).GetFiles("*.aspx", SearchOption.AllDirectories).OrderByDescending(f => f.LastWriteTime).Select(f => f.FullName).ToList();     
             string[] dirs1 = dirs2.ToArray();     
            //Array.Sort(dirs1);       
            if(dirs1 == null || dirs1.Length == 0)
               {
                Response.Write("There are no .aspx files in the specified directory");              
               }
             Response.Write("<ul>");            
            foreach (string dir1 in dirs1.Take(linkCount)) 
            {                                                                           
                  if (now1.Subtract(System.IO.File.GetLastWriteTime(dir1)).TotalMinutes < 10080)
                  {
                  string absoluteUrl1 = dir1.Substring(19);  //28                                        
                string absoluteUrl2 = absoluteUrl1.Replace(@"", @"/");
                  string html1 = File.ReadAllText(dir1);
                  Match m1 = Regex.Match(html1, @"<title>s*(.+?)s*</title>");
                       if (m1.Success)
                       {                          
                          // Response.Write (m1.Groups[1].Value);                         
                          string link1 = "<a href='" +absoluteUrl2+ "'>" + m1.Groups[1].Value + "</a>";                         
                           Response.Write("<li>");   
                           Response.Write(link1);
                           Response.Write("&nbsp;");
                           Response.Write("("); 
                           Response.Write(System.IO.File.GetLastWriteTime(dir1).Date.ToString("MM-dd-yyyy"));
                           Response.Write(")"); 
                        Response.Write("&nbsp;");                                            
                       if (System.IO.File.GetLastWriteTime(dir1).Date.ToString("MM-dd-yyyy") == DateTime.Today.ToString("MM-dd-yyyy"))
                          {                          
                          Response.Write("&nbsp;");
                          Response.Write("<img src='/_resources/images/new.gif' alt='new' />");                           
                           }
                       Response.Write("</li>");                           
                       }
                       else
                       {
                           Response.Write("Page title should not be empty");
                       }
                  } //end if for dates
                  else
                  {                
                   Response.Write("<li>");         
                   Response.Write("No links matching the Criteria");
                   Response.Write("</li>");                               
                  }
                  // end else for dates
            } //end foreach
            Response.Write("</ul>");
           } // end directory exists if   
            else
             {
               Response.Write("The specified directory does not exist");
             }
              // end directory exists else
        } //end try
        catch (Exception e) 
        {
            Response.Write("The process failed: ");
            Response.Write(e.ToString());
        }
   }
}   

最新更新