Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to List all Files in Directory as hyperlink

0.00/5 (No votes)
24 Jan 2010 1  
One day i created many aspx files in one directory, then I wanted to browse those files without going and clicking each one of them, I decided to write small function to create a hyperlink for each file. I thought someone else might benefit from the code, here it is: strDir =...
One day i created many aspx files in one directory, then I wanted to browse those files without going and clicking each one of them, I decided to write small function to create a hyperlink for each file. I thought someone else might benefit from the code, here it is:


strDir = "~/directoryname/"<br>
<br>
Private Sub createHyperlinks(ByVal strDir As String)
    Dim i As Integer = 1
    For Each file As String In Directory.GetFiles(Server.MapPath(strDir))
        If Path.GetFileName(file).EndsWith(".aspx") Then
            Dim lnk As New HyperLink
            PlaceHolder1.Controls.Add(GetLiteral("<br />"))
            lnk.ID = "lnk" & i.ToString()
            lnk.NavigateUrl = strDir & Path.GetFileName(file)
            lnk.Text = Mid(Path.GetFileName(file), 1, (Path.GetFileName(file)).IndexOf("."))
            PlaceHolder1.Controls.Add(lnk)
            i += 1
        End If
    Next
End Sub


Private Function GetLiteral(ByVal text As String) As Literal
    Dim rv As Literal
    rv = New Literal
    rv.Text = text
    Return rv
End Function


And finally here is the html:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>


Hope you like it, thank you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here