Click here to Skip to main content
15,884,298 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

 
QuestionHow can i access Files or directories from web server ?? Bcoz its working only in local System. Pin
Member 1216284513-Jan-21 21:32
Member 1216284513-Jan-21 21:32 
QuestionI wanted to use this in Modal Popup Extender control in VB.Net. Pin
sanket289427-Sep-17 23:30
sanket289427-Sep-17 23:30 
QuestionIt work on local machine but not work when is publish it on IIS and browse it. How can we resolve it? Pin
truptisuruawanshi14-Sep-16 2:04
truptisuruawanshi14-Sep-16 2:04 
QuestionHow to achieve same in MVC Pin
satya4venky31-Aug-16 20:09
satya4venky31-Aug-16 20:09 
This code is in Asp.Net, How can we get the same functionality in MVC?
BugWhen i use it Pin
Markus Kühter14-Dec-15 22:54
Markus Kühter14-Dec-15 22:54 
QuestionAccess Files from Server Pin
Member 1122661831-Mar-15 3:15
Member 1122661831-Mar-15 3:15 
AnswerRe: Access Files from Server Pin
namkan3sparta23-Jun-15 0:31
namkan3sparta23-Jun-15 0:31 
AnswerRe: Access Files from Server Pin
namkan3sparta23-Jun-15 20:21
namkan3sparta23-Jun-15 20:21 
QuestionCould you make it work for Google Chrome? Pin
Member 868511424-Mar-15 7:44
Member 868511424-Mar-15 7:44 
Questionbrowsing directory in network Pin
dharmendra prajapati10may199019-Jan-15 21:07
dharmendra prajapati10may199019-Jan-15 21:07 
QuestionThanks alot Pin
Gerald.Manaka14-Jan-15 3:47
Gerald.Manaka14-Jan-15 3:47 
GeneralMy vote of 3 Pin
Member 41393824-Apr-14 9:26
Member 41393824-Apr-14 9:26 
Suggestionyour code does not display all folder in particular drive in vs2010 Pin
sankmahesh23-Oct-13 23:58
sankmahesh23-Oct-13 23:58 
Questionwill this treeview fetches the data from server folders or local system folders? Pin
aravind0627-Sep-13 20:05
aravind0627-Sep-13 20:05 
AnswerRe: will this treeview fetches the data from server folders or local system folders? Pin
ankurmee31-Jan-14 1:59
ankurmee31-Jan-14 1:59 
QuestionCan u give this project in vb codings ? Pin
Aravindba21-Jul-13 6:21
professionalAravindba21-Jul-13 6:21 
Bugbug on expanding node Pin
Iosif_petre188-Apr-13 10:31
Iosif_petre188-Apr-13 10:31 
QuestionBrowsing Namespace Pin
Franco Cipriano23-Jul-12 4:45
Franco Cipriano23-Jul-12 4:45 
QuestionDirectory Browsing Pin
anandkumar018822-May-12 0:55
anandkumar018822-May-12 0:55 
QuestionVB source Pin
highglow23-Apr-12 6:26
highglow23-Apr-12 6:26 
QuestionNot able to browse network drive Pin
ajitsingh41711-Jan-12 2:09
ajitsingh41711-Jan-12 2:09 
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 

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.