Saturday, May 30, 2009

List Files in a Directory in C#

// List Files in a Directory in C#

// You can get an array of files (their path) by using:
fileArray = Directory.GetFiles(yourPath);

// You can display all of the images in a directory by using:
ArrayList pics = new ArrayList();

string myFile;
string code;
const string YOUR_DIRECTORY = "/images/";

foreach (myFile in Directory.GetFiles(Server.MapPath(YOUR_DIRECTORY), "*.gif"))
{
image = YOUR_DIRECTORY + Path.GetFileName(myFile);
code = <"img src=\"" + image + "\">";

// add the image to your ArrayList
pics.Add(code);
}

// Bind the pics to a DataList
dlImages.DataSource = pics;
dlImages.DataBind;

No comments:

Post a Comment