Hi friend...
Please try the below code .
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;
}
}