代码实现IIS浏览

效果图:

代码实现IIS浏览_第1张图片

 

//调用页面的方法 Response.Redirect("ListReport.aspx?Path="+System.Web.HttpUtility.UrlEncode("数据//高一考试//高一考试", System.Text.Encoding.UTF8)); //弹出窗口显示 Page.ClientScript.RegisterStartupScript(GetType(), "a", "<script>window.open('ListReport.aspx?Path="+System.Web.HttpUtility.UrlEncode("数据", System.Text.Encoding.UTF8)+"','_ListReport');</script>");

页面ListReport.aspx代码:

 

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class ListReport : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { string VirtualPath = Request.Params["Path"].ToString();//虚拟路径 string PhysicPath = Server.MapPath("//") + VirtualPath;//物理路径 Response.Write("<H1>"+Request.Url.Authority+" - //" + VirtualPath + "//</H1><hr><pre>"); if (VirtualPath.LastIndexOf("//") == -1) { Response.Write("<A HREF='//'>[转到父目录]</A></pre><br><br><table border='0' cellpadding='0' cellspacing='0' style='font-family:Verdana;font-size:12px;' width='75%'>"); } else { string FatherVirtualPath = VirtualPath.Substring(0, VirtualPath.LastIndexOf("//")); Response.Write("<A HREF='ListReport.aspx?Path=" + System.Web.HttpUtility.UrlEncode(FatherVirtualPath) + "'>[转到父目录]</A><br><br></pre><table border='0' cellpadding='0' cellspacing='0' style=' font-family:Verdana;font-size:12px;' width='75%'>"); } string[] DirectoryList = GetDirs(PhysicPath); for (int i = 0; i < DirectoryList.Length; i++) { DirectoryInfo Directory_Info = new DirectoryInfo(DirectoryList[i].ToString()); if (Directory_Info.Attributes == FileAttributes.Directory) { string VirtualDirectory = DirectoryList[i].Substring(Server.MapPath("//").Length); string VirtualDirectoryName = VirtualDirectory.Substring(VirtualDirectory.LastIndexOf("//") + 1); Response.Write("<tr><td style='text-align:center;'>" + Directory_Info.CreationTime.ToString("yyyy年MM月dd日 HH:mm") + "</td><td style='text-align:center;'>" + "&lt;目录&gt;</td><td style='text-align:left;'><A HREF='ListReport.aspx?Path=" + System.Web.HttpUtility.UrlEncode(VirtualDirectory) + "'>" + VirtualDirectoryName + "</A></td></tr>"); } } ArrayList FileList = GetFileName(PhysicPath); for (int i = 0; i < FileList.Count; i++) { FileInfo File_Info = new FileInfo(FileList[i].ToString()); double fileLength = ((float)File_Info.Length) / 1024.00; string strLength = fileLength > 512.00 ? ((double)(fileLength / 1024.00)).ToString("0.00") + "M" : fileLength.ToString("0.00") + "K"; string VirtualFile = (FileList[i].ToString()).Substring(Server.MapPath("//").Length); string VirtualFileName = VirtualFile.Substring(VirtualFile.LastIndexOf("//") + 1); Response.Write("<tr><td style='text-align:center;'>" + File_Info.CreationTime.ToString("yyyy年MM月dd日 HH:mm") + "</td><td style='text-align:center;'>" + strLength + "</td><td td style='text-align:left;'><A HREF=http://"+Request.Url.Authority+"//" + VirtualFile + ">" + VirtualFileName + "</A></td></tr>"); } Response.Write(" </table><br><hr>"); } catch (System.Exception ey) { Page.ClientScript.RegisterStartupScript(GetType(), "a", "<script>alert('" + ey.ToString() + "')</script>"); } } /// <summary> /// 重新写标题 /// </summary> /// <param name="output"></param> protected override void Render(HtmlTextWriter output) { string VirtualPath = Request.Params["Path"].ToString();//虚拟路径 output.WriteLine("<script>document.title='" + Request.Url.Authority + "-/" + VirtualPath.Replace('//', '/') + "';</script>"); } private ArrayList GetFileName(string dirPath) { ArrayList list = new ArrayList(); if (Directory.Exists(dirPath)) { list.AddRange(Directory.GetFiles(dirPath)); } return list; } /// <summary> /// // 获取所有文件夹及子文件夹 /// </summary> /// <param name="dirPath">文件夹路径</param> /// <returns>文件夹string[]</returns> private string[] GetDirs(string dirPath) { string[] DirectoryList = Directory.GetDirectories(dirPath); return DirectoryList; } }

你可能感兴趣的:(代码实现IIS浏览)