Search  
   
Browse by Category
 
Netfirms 24/7 Support .: WINDOWS HOSTING .: ASP.NET .: How do I display a directory listing of my web site using an ASP.NET script?

How do I display a directory listing of my web site using an ASP.NET script?

Below is a sample script that may be used to display your website's files and directories:
dirlist.aspx
<%@ Page Language="VB" EnableViewState="False" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
 Sub Page_Load(sender as Object, e as EventArgs)
  Dim strPath   As String = ""
  Dim strQuery  As String
  Dim myDirInfo As DirectoryInfo
  strQuery = txtQuery.Text
  If strQuery = "" Then strQuery = CStr(Request.QueryString("query"))
  txtQuery.Text = strQuery
  lblPath.Text = strPath
  myDirInfo = New DirectoryInfo(Server.MapPath(strPath))
  'Response.Write("*" & strQuery & "*")
  dgFileList.DataSource = myDirInfo.GetFiles("*" & strQuery & "*")
  dgFileList.DataBind()
 End Sub
</script>
<html>
<head>
<title>ASP.NET Directory List (Filtered) Sample</title>
</head>
<body>
<form runat="server">
Find files whose names contain:
<asp:TextBox id="txtQuery" runat="server" /><br />
<input type="submit" value="Find Files" />
</form>
<p>
Some sample queries:
<a href="<%= Request.ServerVariables("URL") %>?query=ary">ary</a>,
<a href="<%= Request.ServerVariables("URL") %>?query=ber">ber</a>,
<a href="<%= Request.ServerVariables("URL") %>?query=0">0</a>,
<a href="<%= Request.ServerVariables("URL") %>?query=1">1</a>,
<a href="<%= Request.ServerVariables("URL") %>">show all files</a>.
</p>
<p>
Contents of <strong><asp:Literal id="lblPath" runat="server" /></strong> that match your query:
</p>
<asp:DataGrid id="dgFileList" runat="server"
 Border                = 5
 BorderColor           = "green"
 CellSpacing           = 0
 CellPadding           = 2
    HeaderStyle-BackColor = "#006600"
    HeaderStyle-ForeColor = "#FFFFFF"
    HeaderStyle-Font-Bold = "True"
    ItemStyle-BackColor   = "#CCFFCC"
    AutoGenerateColumns   = "False"
    >
 <Columns>
  <asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="./{0}" DataTextField="Name" HeaderText="File Name:" />
  <asp:BoundColumn DataField="Length" HeaderText="File Size:" ItemStyle-HorizontalAlign="Right" />
  <asp:BoundColumn DataField="LastWriteTime" HeaderText="Date Created:" />
 </Columns>
</asp:DataGrid>
<hr />
</body>
</html>
NOTE: For additional ASP.NET support and resources we recommend the following web sites:

How helpful was this article to you?


.: Powered by Lore 1.5.6
Visit Netfirms.com Web Hosting | Copyright © 1998 - 2009 Netfirms, Inc. All Rights Reserved.