AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=118&pId=-1
Displaying Files with a Hyperlink to download them
page
by Rajiv R.
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25854/ 28

Introduction

Someone requested for help on displaying all the files in a directory with a Hyperlink to download it, and this is in reponse to that!

It quiet simple! We use the DirectoryInfo and FileInfo classes of System.IO namespace.

System.IO.DirectoryInfo di=new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\WebApplication2");

Code

We start with creating a DirectoryInfo object and the directory whose files has been to displayed is passed to the constructor.

System.IO.FileInfo[] fiArr=di.GetFiles();

Next, we call the GetFiles() method of the DirectoryInfo object. It returns a array of FileInfo objects.

foreach (System.IO.FileInfo fi in fiArr)
{
    Response.Write("<a href='"+fi.Name+"'>"+fi.Name+ "<br>") ;
}

Last, for each FileInfo object, we construct a anchor tag.

Alternatively, We can create Hyperlink objects,set the Text and NavigateURL properties and add it to the page.

HyperLink hl=new HyperLink(); hl.NavigateUrl=fi.Name; hl.Text=fi.Name; Page.Controls.Add(hl);

VB.NET Code:

Dim di As New System.IO.DirectoryInfo("C:\Inetpub\wwwroot\WebApplication2\")
Dim fiArr As System.IO.FileInfo() = di.GetFiles()
Dim fi As System.IO.FileInfo

For Each fi In fiArr
    Response.Write("<a href='" + fi.Name + "'>" + fi.Name + "<br>")
Next fi

Send your comments to rajiv.raj@wipro.com


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 10:40:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search