Click here to Skip to main content
Click here to Skip to main content

How to List all Files in Directory as hyperlink

, 24 Jan 2010
Rate this:
Please Sign up or sign in to vote.
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/"
 
    
 
    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)

About the Author

dhamenha
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 24 Jan 2010
Article Copyright 2010 by dhamenha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid