通过ASP.NET解析IIS文档根并获取文件的最后修改日期
我正在使用ASP.NET 我的任务是遍历文档根C:\inetpub\wwwroot的所有文件夹和文件 查找过去7天内修改过的所有.aspx文件=>从这些文件中获取“title”节点的值=>输出“title”值和上次修改日期 任何带有代码样本的指示都将受到欢迎 谢谢通过ASP.NET解析IIS文档根并获取文件的最后修改日期,asp.net,file,loops,directory,Asp.net,File,Loops,Directory,我正在使用ASP.NET 我的任务是遍历文档根C:\inetpub\wwwroot的所有文件夹和文件 查找过去7天内修改过的所有.aspx文件=>从这些文件中获取“title”节点的值=>输出“title”值和上次修改日期 任何带有代码样本的指示都将受到欢迎 谢谢 海克 以下是我为上述问题编写的解决方案: using System.Collections; using System.Collections.Specialized; using System.Xml; using System.G
海克 以下是我为上述问题编写的解决方案:
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());
}
}
}
使用系统集合;
使用System.Collections.Specialized;
使用System.Xml;
利用制度全球化;
使用System.IO;
Net系统;
使用系统配置;
使用系统数据;
使用System.Data.SqlClient;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Linq;
使用System.Xml.Linq;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
公共部分类DirectoryIterator:System.Web.UI.Page
{
公共void生成器链接(字符串网页、int链接计数)
{
DateTime now1=DateTime.Now;
字符串webpathnoslash=webpath.Substring(1);
字符串路径=Server.MapPath(webpathnoslash);
尝试
{
if(目录存在(路径))
{
//字符串[]dirs1=Directory.GetFiles(路径“*.aspx”,SearchOption.AllDirectories);
var dirs2=new DirectoryInfo(path).GetFiles(“*.aspx”,SearchOption.AllDirectories).OrderByDescending(f=>f.LastWriteTime).Select(f=>f.FullName).ToList();
字符串[]dirs1=dirs2.ToArray();
//数组.Sort(dirs1);
if(dirs1==null | | dirs1.Length==0)
{
Write(“指定目录中没有.aspx文件”);
}
响应。写入(“”);
foreach(dirs1.Take中的字符串dir1(linkCount))
{
if(now1.Subtract(System.IO.File.GetLastWriteTime(dir1)).TotalMinutes<10080)
{
字符串absoluteUrl1=dir1.子字符串(19);//28
字符串absoluteUrl2=absoluteUrl1.Replace(@“\”,@“/);
字符串html1=File.ReadAllText(dir1);
Match m1=Regex.Match(html1,@“\s*(.+?)\s*”);
如果(m1.成功)
{
//Response.Write(m1.Groups[1].Value);
字符串link1=“”;
回答。写(“- ”);
回答。写(link1);
回答。写(“”);
答复:填写(“”);
Write(System.IO.File.GetLastWriteTime(dir1.Date.ToString(“MM dd yyyy”));
答复:填写(“)”;
回答。写(“”);
if(System.IO.File.GetLastWriteTime(dir1.Date.ToString(“MM dd yyyy”)==DateTime.Today.ToString(“MM dd yyyy”))
{
回答。写(“”);
回答。写(“”);
}
回答。写(“
”);
}
其他的
{
响应。写入(“页面标题不应为空”);
}
}//如果是日期,则结束
其他的
{
回答。写(“- ”);
回复。写下(“没有符合标准的链接”);
回答。写(“
”);
}
//结束约会
}//结束foreach
响应。写入(“
”);
}//如果
其他的
{
Write(“指定的目录不存在”);
}
//结束目录存在于其他目录中
}//结束尝试
捕获(例外e)
{
Response.Write(“进程失败:”);
Response.Write(例如ToString());
}
}
}