TreeView绑定硬盘文件
有时候需要做一个像资源管理器一样的东西来查看服务器中的文件,怎么做呢,我写了一个
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BdTreeView(AppDomain.CurrentDomain.BaseDirectory,TreeView1.Nodes);
}
}
public void BdTreeView(string dir, TreeNodeCollection tc)
{
DirectoryInfo di = new DirectoryInfo(dir);
DirectoryInfo[] sunDir = di.GetDirectories();
FileInfo[] ffi = di.GetFiles();
foreach (DirectoryInfo i in sunDir)
{
TreeNode tn = new TreeNode();
tn.Text = Path.GetFileName(i.FullName);
tc.Add(tn);
BdTreeView(i.FullName, tn.ChildNodes);
}
foreach (FileInfo j in ffi)
{
TreeNode fileNode = new TreeNode();
fileNode.Text = System.IO.Path.GetFileName(j.FullName);
tc.Add(fileNode);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BdTreeView(AppDomain.CurrentDomain.BaseDirectory,TreeView1.Nodes);
}
}
public void BdTreeView(string dir, TreeNodeCollection tc)
{
DirectoryInfo di = new DirectoryInfo(dir);
DirectoryInfo[] sunDir = di.GetDirectories();
FileInfo[] ffi = di.GetFiles();
foreach (DirectoryInfo i in sunDir)
{
TreeNode tn = new TreeNode();
tn.Text = Path.GetFileName(i.FullName);
tc.Add(tn);
BdTreeView(i.FullName, tn.ChildNodes);
}
foreach (FileInfo j in ffi)
{
TreeNode fileNode = new TreeNode();
fileNode.Text = System.IO.Path.GetFileName(j.FullName);
tc.Add(fileNode);
}
}
}
}