Getting List of Files in a Folder

  • Thread starter Thread starter chuckdfoster
  • Start date Start date
C

chuckdfoster

I am not sure if this is even possible, but I figured I'd ask some experts.
How would I use an ASP.NET page to get a listing of all of the files that
are in a folder. I am working on a page that can show a group of users if
their reports are in a folder (kind of like a report distribution page). If
that is possible then how could I put a link on the page to that file so the
user could open it.

Thanks!
 
DirList.aspx
=========
<script language="vb" runat="server">
Sub Page_Load()
Dim StoreFile As System.IO.Directory
Dim Files As String()
Dim File As String
Files = StoreFile.GetFiles("drive:\directory\path\", "*")
For Each File In Files
Response.Write(File & "<br>")
Next
End Sub
</script>

<html>
<head><title>File List</title></head>
<body>

</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
=====================
 
I am not sure if this is even possible, but I figured I'd ask some experts.
How would I use an ASP.NET page to get a listing of all of the files that
are in a folder. I am working on a page that can show a group of users if
their reports are in a folder (kind of like a report distribution page).
If
that is possible then how could I put a link on the page to that file so
the
user could open it.

No problem at all. Take a look at the System.IO namespace, instantiate a
Directory object and then call its GetFiles method e.g.

using System.IO;

string[] astrFiles = Directory.GetFiles(<directory name goes here>, "*.*");
foreach (string strFile in astrFiles)
{
// write out a hyperlink to the file which the users can click
}

Make sure that the account that ASP.NET is running under needs to be able to
"see" the directory and have sufficient permissions on it to get a listing
of its files.
 
Chuck:

check out System.IO.Directory.GetDirectories and
System.IO.Directory.GetFiles

for each file as string in System.IO.Directory.GetFiles("c:\repository\")

next

If there's a folder tree (ie, you need to recurse), you'll need a recursive
function. How to put a link to the file depends on whether the file exists
in the web directory or in some other folder. If it's in the web
directory, you can simply link to it. Otherwise look at the
REsponse.WriteFile method.

Karl
 
Juan:
Your use of a shared field like that struck me odd. System.IO.Directory
can't be created (constructor is private, class is sealed and all members
are static) so, in my opinion you should always use System.IO.Directory.XXX
instead of creating a field and then doing someField.XXX. In VB.Net you can
do:

Dim x as System.IO.Directory

but in C# you can't do the same, ie:

System.IO.Directory x = System.IO.Directory;

Might just be a personal preference of mine, all I could quickly dig up was
this discussion:
http://dotnetjunkies.com/WebLog/grant.killian/archive/2004/03/02/8249.aspx


Karl
 
Looks like we found something which can be
done in VB.NET which can't be done in C#.

;-)

No errors, no warnings. It just works.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
=====================
 
Back
Top