How to List all Files in Directory as hyperlink
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 FunctionAnd finally here is the html:
Hope you like it, thank you.