我正在使用 ASP.NET
我的任务是遍历 Document Root C:\inetpub\wwwroot 的所有文件夹和文件,找到过去 7 天内修改过的所有 .aspx 文件 => 从这些文件中获取“标题”节点的值 => “标题”的输出值和上次修改日期。
任何带有代码示例的方向都将不胜感激。
谢谢哈克。
这是我为上述问题编写的解决方案:
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(" ");
Response.Write("(");
Response.Write(System.IO.File.GetLastWriteTime(dir1).Date.ToString("MM-dd-yyyy"));
Response.Write(")");
Response.Write(" ");
if (System.IO.File.GetLastWriteTime(dir1).Date.ToString("MM-dd-yyyy") == DateTime.Today.ToString("MM-dd-yyyy"))
{
Response.Write(" ");
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());
}
}
}