How do I get a list of files from a web directory?

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

+2


source to share


4 answers


About the "get this list in C #" part:



foreach (string filename in 
    Directory.GetFiles(
        Server.MapPath("/"), "*.jpg", 
        SearchOption.AllDirectories))
{
    Response.Write(
        String.Format("{0}<br />", 
            Server.HtmlEncode(filename)));
}

      

+3


source


This is an interesting topic that I researched recently. As you know, you can access BITS through COM, but here are some projects to make it easier:

SharpBITS.NET Formal design wrapper
with friendly background Intelligent Transfer Service (BITS)



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

I experimented with the code in the CodeProject link and it seems to work quite well. The CodePlex project looks very good, but I haven't tried it.

0


source


Ok, if the web server allows you to list the files in the directory in question, you can go.

Unfortunately, there is no standard on how the web server should return the list to you. It is common in HTML, but HTML is not always formatted on different web servers.

If you want to always download files from the same directory on the same web server, simply use the "view source" while in your web browser directory. Then try writing a small regex that will capture all filenames from the HTML source.

Then you can create a WebClient, query the directory url, parse the response to get the filenames with your regex, and then process the files with your BITS client

Hope it helps

0


source


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

      

0


source







All Articles