Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends ,

I am Sidhanta. I have a button and a text box.While clicking on the button it will open a Directory Dialog and the path will be copied to text box. I got some solution but they are not working.
C#
Dialog.FolderBrowser fBrowser = new Dialog.FolderBrowser();
           fBrowser.Description = "Select the Folder that you like.";
           fBrowser.StartLocation = OpenFileDialog.FolderBrowser.fbFolder.Desktop;
           fBrowser.Style = Dialogs.FolderBrowser.fbStyles.RestrictToSubfolders;
           //DialogResult result = fBrowser.ShowDialog();
           if (fBrowser.ShowDialog() == DialogResult.OK)
           {
               txtDirectory.Text = fBrowser.DirectoryPath;

           }


As Dialog is coming under System.Windows.Forms. But it gives error as Type or namespace could not found(are you missing a using directive or an assembly reference).
Kindly guides me to get the exact solution.

With Regards
Sidhanta Tripathy
Posted
Updated 29-Jun-12 1:10am
v2
Comments
Vani Kulkarni 29-Jun-12 7:11am    
This is error is related to Windows application. Are you trying to achieve your requirement in Windows or Web Application?
sidhanta tripathy 29-Jun-12 7:15am    
Hi Kulkarni ,
I have tried it in web.

With Regards
Sidhanta Tripathy
Jαved 29-Jun-12 7:19am    
have you referenced System.IO Namespace?
sidhanta tripathy 29-Jun-12 7:20am    
Hi Javed ,
Yes I have used System.IO.

With Regards
Sidhanta Tripathy

1 solution

Hi friend...

Please try the below code .
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Data;
using System.Configuration;

public partial class Fileinfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode onjparent = new TreeNode("D:\\", "D:\\");
            onjparent.PopulateOnDemand = true;
            TreeView1.Nodes.Add(onjparent );
            TreeView1.CollapseAll();

       }

        Errorlbl.Visible = false;
        TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
        TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);

    
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {

    }
    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {

        if (e.Node.Value.EndsWith("\\"))
        {

            Addnodes(e.Node.Value, e.Node);

        }
    }

    private TreeNode Addnodes(string path, TreeNode parentnode)
    {
       

        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;

    }


    protected void btnbrows_Click(object sender, ImageClickEventArgs e)
    {
        TreeView1.Nodes.Clear();

        if (UpdateBrowseTextBoxWithSlash())
        {
            TreeNode onjparent = new TreeNode(txtbrow.Text, txtbrow.Text);
            onjparent.PopulateOnDemand = true;
            TreeView1.Nodes.Add(onjparent);
            TreeView1.CollapseAll();



        }

        else {


            Errorlbl.Visible = true;

            Errorlbl.Text = "please enter valid path";

        }

    }

    private bool UpdateBrowseTextBoxWithSlash()
    {
        if (txtbrow.Text.Length != 0)

        { 
			if(
                -1==txtbrow.Text.LastIndexOf (".") && !txtbrow.Text.Substring (txtbrow.Text.Length-1,1).Equals ("/") &&

                !txtbrow.Text.Substring (txtbrow.Text.Length-1,1).Equals ("\\")
                )
            {
                if (txtbrow.Text.Substring(0,1).Equals ("\\")|| -1!=txtbrow.Text.IndexOf(":\\"))
                txtbrow.Text +="\\";

            else

            txtbrow.Text +="/";
                
            return System.IO.Directory.Exists(txtbrow.Text);

            }
            else if(txtbrow.Text.LastIndexOf(".") >0)
            {
            
                return System.IO.File.Exists(txtbrow.Text);

            }
        
        }

        return true;
   
    
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        txtbrow.Text = TreeView1.SelectedValue;

    }
}
 
Share this answer
 
Comments
sidhanta tripathy 2-Jul-12 6:22am    
Hi,
There is no such a keword Filelist. Can you please explain the term for Filelist.
Asridhar 28-Jan-13 7:54am    
please,povide details on Filelist.
nadir siddiqui 13-Jan-14 5:47am    
Where I can get FileList object in asp.net
Praseeda VP 31-Jul-15 12:30pm    
Hi,
Nice aricle, please refer the namespace for the class 'FileList'.

Thanks in advace..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900