tags:

views:

344

answers:

4

How do I get a a list of files from a web directory? If I access the web directory URL the internet browser list all the files in that directory. Now I just want to get that list in C# and download them in BITS (Background Intelligent Transfer Service) .

A: 

This is an interesting topic I investigated fairly recently. As you know you can access BITS via COM, but here are a couple projects to make it easier:

SharpBITS.NET
Forms Designer Friendly Background Intelligent Transfer Service (BITS) wrapper

This article on MSDN might be a bit more than you want to know.

I experimented with the code in the CodeProject link and it seemed to work reasonably well. The CodePlex project looks really good but I haven't tried it.

Jay Riggs
Note: I am using SharpBITS.net in my project. I agree seems to be really nice.
Eric
+1  A: 

About "get that list in C#" part:

foreach (string filename in 
    Directory.GetFiles(
        Server.MapPath("/"), "*.jpg", 
        SearchOption.AllDirectories))
{
    Response.Write(
        String.Format("{0}<br />", 
            Server.HtmlEncode(filename)));
}
Rubens Farias
Thanks for the response. This is a great way to create a list, but I don't want to create a list I want to read and parse the default IIS list of files in a directory.
Eric
@Eric, sorry I didn't understood what are you trying to accomplish; can you please elaborate your problem?
Rubens Farias
A: 

Well, if the Web Server allows to list the files the directory in question, you're good to go.

Unfortunately, there's no standard on how the web server should return you the list. It is often in HTML, but the HTML is not always formatted the same across multiple web servers.

If you want to download files always from the same directory on the same web server, just do a "view source" while being in the directory in your web browser. Then try to write a small regular expression that will grab every file names from the HTML source.

You can then create a WebClient, request the directory URL, parse the response to get the file names with your regular expression, then process the files with your BITS client

Hope this helps

Mike Gleason jr Couturier
A: 
private void ListFiles()
{

    //get the user calling this page 
    Gaf.Bl.User userObj = base.User;
    //get he debug directory of this user
    string strDebugDir = userObj.UserSettings.DebugDir;
    //construct the Directory Info directory 
    DirectoryInfo di = new DirectoryInfo(strDebugDir);
    if (di.Exists == true)
    {

        //get the array of files for this 
        FileInfo[] rgFiles = di.GetFiles("*.html");
        //create the list ... .it is easier to sort ... 
        List<FileInfo> listFileInfo = new List<FileInfo>(rgFiles);
        //inline sort descending by file's full path 
        listFileInfo.Sort((x, y) => string.Compare(y.FullName, x.FullName));
        //now print the result 
        foreach (FileInfo fi in listFileInfo)
        {
            Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>");
        } //eof foreach
    } //eof if dir exists

} //eof method 
YordanGeorgiev