Click here to Skip to main content
15,886,825 members
Articles / Web Development / ASP.NET
Tip/Trick

How to List all Files in Directory as hyperlink

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Jan 2010CPOL 23.7K   9   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis not working properly it create the link in the literal ... Pin
danmor49817-Jun-10 10:51
danmor49817-Jun-10 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.