Click here to Skip to main content
15,884,237 members
Articles / Web Development / ASP.NET
Article

Directory Browsing in ASP.Net 2.0

Rate me:
Please Sign up or sign in to vote.
2.93/5 (11 votes)
15 Apr 2008CPOL1 min read 183.8K   9.9K   48   39
An article on browsing via directories or HTTP, with ASP.NET using Tree View Control
Download Browsing.zip - 18.72 KB
Screenshot - main.JPG

Introduction

The file uploder is the control which help to select the file but not the path of the file or directory and also we can't make a Active X Control for this so with the help of pop-up we provide the user to select file or directory path in the intranet. This pop-up look same like the directory browing in desktop application. 

Background

I read this article http://www.codeproject.com/KB/aspnet/Browsing.aspx which is in ASP.net 1.1. I have changed the folder structure with tree view.

Using the code

Create a C# web application in asp.net 2.0 and add BrowseDirectory.aspx and add following files into your app_code folder

  • WebForm1_aspx_cs_FileList.cs
  • WebForm1_aspx_cs_PathHelper.cs

and create a textbox and button on which webform you want to use it. Add onClientClick of the button and set OnClientClick="showDirectory();" . Add this script into yout web form for calling the browsing page.

function showDirectory()
             {
                document.all.TextBox1.value= window.showModalDialog("browseDirectory.aspx",
                            'jain',
                            "dialogHeight: 560px; dialogWidth:360px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: No;"); 
                return false;
              }

 

 

Explanation of code blocks

Add Node method in the BrowseDirectory.aspx which add the tree node of the directory and files. FileList Constructor takes path and the filters for the files

FileList objList = new FileList(path, "*.*");
TreeNode node = new TreeNode(path,path);
for(int index = 0 ; index < objList.Directories.Length ; index++)
{
    string directory = objList.Directories[index];
    TreeNode objChildNode = new TreeNode(directory, path + "\\" + directory + "\\");
    objChildNode.PopulateOnDemand = true;
    objChildNode.Target = "_blank";

    parentNode.ChildNodes.Add(objChildNode);
}
foreach (string file in objList.Files)
{
    TreeNode objChildNode = new TreeNode(file, path + "\\" + file);
    parentNode.ChildNodes.Add(objChildNode);
}
return node;

For selecting the path of the file or directory use the selection event of the tree view

void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    _browseTextBox.Text = TreeView1.SelectedValue;
}

Expanding is base on the directory selection.

 void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
    if (e.Node.Value.EndsWith("\\"))
    {
        AddNodes(e.Node.Value, e.Node);
    }


}

Finally when you select the path then go back to the parent page with this path

<script language="javascript">
    <!--
    function SelectAndClose()
        {
            txtValue = document.getElementById('_browseTextBox');

            window.returnValue = txtValue.value;
            window.close();
            return false;
        }


    -->
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Nagarro Software Pvt Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUn Know in Fire Fox Pin
Phan Van Thao25-Sep-11 17:06
Phan Van Thao25-Sep-11 17:06 
GeneralQuestion [modified] Pin
Atul Naik21-Sep-11 2:03
Atul Naik21-Sep-11 2:03 
GeneralDirectory browser Pin
srinivas prabhu20-Aug-10 1:37
srinivas prabhu20-Aug-10 1:37 
GeneralRe: Directory browser Pin
Rupesh Burad22-Aug-10 18:48
Rupesh Burad22-Aug-10 18:48 
GeneralRe: Directory browser Pin
selllllllllllllll8-Sep-11 1:50
selllllllllllllll8-Sep-11 1:50 
GeneralMy vote of 1 Pin
zdlik5-Feb-10 12:36
zdlik5-Feb-10 12:36 
QuestionThanks....? Pin
PankajRai.Net13-Nov-09 21:37
PankajRai.Net13-Nov-09 21:37 
GeneralDirectory Browsing in ASP.Net 2.0 Pin
ab_sdfdsfsdf20-Oct-09 7:31
ab_sdfdsfsdf20-Oct-09 7:31 
I download code and run fine with browse directory on c drive on local machine but when i develop on other server and try to browse it open c drive of the development server is there any way to browse always the local machine c drive.and not the development server or production server drive.

thanks
GeneralHi! Pin
weiiis11-Aug-09 3:42
weiiis11-Aug-09 3:42 
GeneralThanks Pin
Intellect22-Mar-09 19:32
Intellect22-Mar-09 19:32 
GeneralMy vote of 1 Pin
joshuaemory31-Dec-08 5:21
joshuaemory31-Dec-08 5:21 
QuestionSource code Pin
clbmribas12-Jul-08 9:59
clbmribas12-Jul-08 9:59 
QuestionNeed Source Pin
Jeff Bowman27-Dec-07 18:10
professionalJeff Bowman27-Dec-07 18:10 
GeneralRe: Need Source Pin
Rupesh Burad15-Apr-08 23:27
Rupesh Burad15-Apr-08 23:27 
GeneralRe: Need Source Pin
Jeff Bowman19-Apr-08 15:53
professionalJeff Bowman19-Apr-08 15:53 
QuestionSourcecode Pin
knoami12-Dec-07 6:50
knoami12-Dec-07 6:50 
GeneralRe: Sourcecode Pin
Rupesh Burad15-Apr-08 23:27
Rupesh Burad15-Apr-08 23:27 
GeneralRe: Sourcecode Pin
knoami16-Apr-08 3:50
knoami16-Apr-08 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.