Getting List of Files in a Folder

Discussion in 'ASP .Net' started by chuckdfoster, Feb 7, 2005.

  1. chuckdfoster

    chuckdfoster Guest

    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!

    --
    Chuck Foster
    Programmer Analyst
    Eclipsys Corporation - St. Vincent Health System
     
    chuckdfoster, Feb 7, 2005
    #1
    1. Advertising

  2. 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
    =====================

    "chuckdfoster" <> wrote in message
    news:%...
    >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!
    >
    > --
    > Chuck Foster
    > Programmer Analyst
    > Eclipsys Corporation - St. Vincent Health System
    >
    >
     
    Juan T. Llibre, Feb 7, 2005
    #2
    1. Advertising

  3. chuckdfoster

    Mark Rae Guest

    "chuckdfoster" <> wrote in message
    news:%...

    >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.
     
    Mark Rae, Feb 7, 2005
    #3
  4. chuckdfoster

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "chuckdfoster" <> wrote in message
    news:%...
    > 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!
    >
    > --
    > Chuck Foster
    > Programmer Analyst
    > Eclipsys Corporation - St. Vincent Health System
    >
    >
     
    Karl Seguin, Feb 7, 2005
    #4
  5. chuckdfoster

    Karl Seguin Guest

    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
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Juan T. Llibre" <> wrote in message
    news:...
    > 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
    > =====================
    >
    > "chuckdfoster" <> wrote in message
    > news:%...
    > >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!
    > >
    > > --
    > > Chuck Foster
    > > Programmer Analyst
    > > Eclipsys Corporation - St. Vincent Health System
    > >
    > >

    >
    >
     
    Karl Seguin, Feb 7, 2005
    #5
  6. 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
    =====================

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > 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
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >> 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
    >> =====================
    >>
    >> "chuckdfoster" <> wrote in message
    >> news:%...
    >> >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!
    >> >
    >> > --
    >> > Chuck Foster
    >> > Programmer Analyst
    >> > Eclipsys Corporation - St. Vincent Health System
    >> >
    >> >

    >>
    >>

    >
    >
     
    Juan T. Llibre, Feb 8, 2005
    #6
  7. chuckdfoster

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:%...

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


    Wow!
     
    Mark Rae, Feb 8, 2005
    #7
  8. chuckdfoster

    chuckdfoster Guest

    Thanks to everyone! This is all great help.
    Chuck
    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > 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
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "chuckdfoster" <> wrote in message
    > news:%...
    > > 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!
    > >
    > > --
    > > Chuck Foster
    > > Programmer Analyst
    > > Eclipsys Corporation - St. Vincent Health System
    > >
    > >

    >
    >
     
    chuckdfoster, Feb 8, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dante
    Replies:
    0
    Views:
    360
    Dante
    May 11, 2004
  2. Replies:
    4
    Views:
    2,158
  3. Lucky
    Replies:
    2
    Views:
    1,052
    Kristofer Gafvert
    Oct 18, 2006
  4. Replies:
    5
    Views:
    336
    Alex Martelli
    Apr 5, 2007
  5. Replies:
    7
    Views:
    409
    Gabriel Genellina
    Sep 25, 2007
Loading...

Share This Page