Folder browsing in ASP.net

10,741

Solution 1

No, there is no inbuilt control for this. It is not a normal requirement cause most site don't let their users see their file structures.

Building a user control that does this will be simple though.

I suggest using a TreeView asp.net control, attached to your datasource where you have listed the files.

This sample on binding a treeview should get you started.

You can populate your data using

var path = Server.MapPath("/");
var dirs = Directory.[EnumerateDirectories][2](path);
var files = Directory.[EnumerateFiles][3](path );

Finally to make it look like a dialog, you could use the jQuery UI dialog component.

Solution 2

The solution I have found is, this is just for anyone looking for answer:-

protected void browse_Click(object sender, EventArgs e) {

        Thread thdSyncRead = new Thread(new ThreadStart(openfolder));
        thdSyncRead.SetApartmentState(ApartmentState.STA);
        thdSyncRead.Start();

    }
    public void openfolder()
    {

        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();

        string selectedfolder = fbd.SelectedPath;


        string[] files = Directory.GetFiles(fbd.SelectedPath);
        System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message");

    }

Solution 3

Doing this in a web application is tricky. You would have to enumerate the folders on the server that you want to browse (presumably this is the same server that's running the web application), and then present that hierarchy to the user to select a folder. If it's not too big a hierarchy, you could just enumerate the whole bunch up front, and display it in a tree. If it's big for that, you could use an Ajax approach: select the top-level folder, then send an Ajax request to get the next level, and so on.

To enumerate the folders, you'll need to walk the filesystem yourself. See http://msdn.microsoft.com/en-us/library/dd997370(v=vs.100).aspx for one way.

Share:
10,741
user1594987
Author by

user1594987

Updated on June 04, 2022

Comments

  • user1594987
    user1594987 almost 2 years

    What I'm trying to do here is to allow my user to select a path in a data server on a network, so that I could generate a configuration file.

    I hope to be able to replicate the function of OpenFileDialog() on my asp.net page. However this function does not exist on asp.net, and I do know that there is this control in asp.net call FileUpload. But what I required here, is just the path/directory for the folder. I do not require my files to be uploaded.

    How can it be done?

  • VishwajeetMCA
    VishwajeetMCA over 9 years
    For above code to work the following references have to be included using System.Threading;using System.Windows.Forms;
  • Ahmed Eissa
    Ahmed Eissa over 4 years
    #VishwajeetMCA, This code is not working like that if you only add the references (in code only). But it will work if you right-click on the "References" and select "Add References" and add "System.Windows.Forms" by ticking the tick box near to it.