List the contents of a folder and link to each file

Iterating the contents of a folder is straightforward using classes from System.IO, and listing them as links to the actual file just requires a bit of html added to each file name.

[C#]
using System.IO;
using System.Text;

DirectoryInfo dir;
StringBuilder sb  = new StringBuilder();
FileInfo[] files;

dir = new DirectoryInfo(Server.MapPath("."));
files = dir.GetFiles();
foreach (FileInfo f in files)
{
  sb.Append("<a href=\"" + f.Name.ToString() + "\">");
  sb.Append(f.Name.ToString() + "</a><br />");
}
Literal1.Text = sb.ToString();
[VB]
Imports System.Text
Imports System.IO

Dim dir As DirectoryInfo
Dim sb As StringBuilder = New StringBuilder()
Dim files() As FileInfo 'array of fileinfo objects

dir = New DirectoryInfo(Server.MapPath("."))

files = dir.GetFiles()
For Each f As FileInfo In files
  sb.Append("lt;a href=""" + f.Name.ToString() + """>")
  sb.Append(f.Name.ToString() + "lt;/a>lt;br />")
Next
Literal1.Text = sb.ToString()

Date Posted:
Last Updated:
Posted by:
Total Views to date: 18383

2 Comments

- Alan Fisher

It looks like Literal1 is not defined.

Literal1.Text = sb.ToString();

- Mike

@Alan,

Yeah - you need to add a Literal control to your aspx file (which isn't featured in the article).
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to forums.asp.net instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

Lorenzo 3/26/2015 8:21 AM
In response to iTextSharp - Introducing Tables
Hi Mike How can I add padding to all cells in the table? Kind Regards Lorenzo...

Satyabrata Mohapatra 3/25/2015 8:11 AM
In response to How To Send Email In ASP.NET MVC
Great article. Simple and up to the point....

Afzaal Ahmad Zeeshan 3/24/2015 8:17 PM
In response to How To Send Email In ASP.NET MVC
A great way to teach the MVC framework for sending the emails... Also, what I found helpful was the...

Jim H 3/24/2015 2:32 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Model Binding And Forms
Thank you. This helps....

wazz 3/22/2015 5:48 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
great info!!...

rael 3/21/2015 8:53 PM
In response to Getting the identity of the most recently added record
I spent hours trying to figure how to achieve this in C#. This article helped me. Thanks a lot...

Stephen 3/21/2015 8:48 PM
In response to Ajax with Classic ASP using jQuery
This was very helpful, thanks:)...

patrick voes 3/19/2015 10:19 AM
In response to iTextSharp - Introducing Tables
Thank you! very helpfull....

Bigmachini 3/19/2015 6:13 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
This just made my day, afternoon, evening, night... was looking for a way of doing this without to a...

Bobbyg 3/19/2015 4:14 AM
In response to HTML Helpers For Forms In Razor Web Pages
Nice article. There are bugs in MVC 5 with dropdowns retaining values from other screens and them. I...