Additional Info

Recent Comments

Vijay 5/25/2015 2:12 PM
In response to How To Send Email In ASP.NET MVC
Thanks...

RR 5/25/2015 8:58 AM
In response to Web Pages - Efficient Paging Without The WebGrid
@MIke.. is it possible to combine pagination and filter (change category ) in the script? Thanks...

Irfan Khan 5/25/2015 6:19 AM
In response to Highlighting Keywords Found In Search Results
This is truely helpful. Thank you for the brilliant tip....

Manoj Kulkarni 5/25/2015 4:46 AM
In response to Custom TagHelpers in ASP.NET MVC 6
Thank you for nice article. Really helpful....

Sean 5/21/2015 4:20 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Thank you for the tutorial. Saving to the database works, but the save FilePath is not working. It...

KrASh 5/19/2015 11:10 PM
In response to Adding Search
Hello Mike, I have a question about the second search. In one line it has a where with the title and...

KrASh 5/19/2015 11:04 PM
In response to Accessing Your Model's Data from a Controller
Thanks, I had some silly mistake, can't remember now what it was (had some personal problems last i...

Satyabrata Mohapatra 5/18/2015 4:29 PM
In response to Error Handling in ASP.NET Razor Web Pages
Great article. Learned a lot....

Corstian Boerman 5/13/2015 12:59 PM
In response to ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
It looks as if this technique doesn't work anymore. (As of the VS 2015 RC release) When adding it...

Alvin 5/12/2015 1:20 PM
In response to Razor Web Pages E-Commerce - Adding A Shopping Cart To The Bakery Template Site
Maybe ability to update quantity of the items once you are in the "Review Cart" step. Also, if on...

Share

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: 19198

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).