日期:2014-05-18  浏览次数:20480 次

TreeVIew显示磁盘某路径的所有文件夹及文件
请问下,在net中,怎么才能让TreeVIew显示磁盘某路径下的所有文件夹及文件
类似资源管理器的功能
以web形式的
懂这个的朋友能否给个例子
发送到:chinaruan01@hotmail.com
再次谢谢了

------解决方案--------------------
我刚做成的,和你的要求一样 ! 拿去参考参考把
public partial class treeviewTest : System.Web.UI.Page
{
public ArrayList a1 = new ArrayList();
DataTable fileListTable; //存储全部文件夹信息的datatable
public int Count = 0; //全局计数器

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
builfileListTable();
String path = Server.MapPath( "~/技术资料区/ ");
fillfileListTable(-1, 0, "技术资料区 ");
DirectoryInfo dir = new DirectoryInfo(path);
GetAllDirList(dir, 0);

//根据表的数据递归生成树节点
TreeView1.Nodes.Clear();
TreeNode Node = new TreeNode();
Node.Text = (fileListTable.Rows[0])[ "fullname "].ToString();
Node.Value = (fileListTable.Rows[0])[ "ID "].ToString();
//Node.Expanded = true;
TreeView1.Nodes.Add(Node);
AddTree(0, Node);
Session[ "downfileList "] = fileListTable;
TreeView1.ParentNodeStyle.NodeSpacing = 5;
TreeView1.LeafNodeStyle.NodeSpacing = 5;
}
}


private void AddTree(int ParentID, TreeNode pNode)
{

DataView dvTree = new DataView(fileListTable);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[parentID] = " + ParentID;

foreach (DataRowView Row in dvTree)
{
if (Convert.ToInt32(Row[ "type "].ToString())==0) //属于文件夹属性
{
TreeNode Node = new TreeNode();
//添加当前节点的子节点
Node.Text = getLastDirName(Row[ "fullname "].ToString());
Node.Value = Row[ "ID "].ToString();
pNode.ChildNodes.Add(Node);
//Node.Expanded = true;
AddTree(Int32.Parse(Row[ "ID "].ToString()), Node); //再次递归
}
}

}

private string getLastDirName(string sourceStr)
{
string[] strArr;

strArr=sourceStr.Split(new Char [] { '\\ '});
return strArr[strArr.GetLength(0) - 1];
}


private void fillfileListTable(Int32 pid, Int32 isFile, string fullname,
string name,string ext,DateTime wtime,long size,string content)
{
DataRow dRow = fileListTable.NewRow();

dRow[ "parentID "] = pid;
dRow[ "type "] = isFile;
dRow[ "fullname "] = fullname;
dRow[ "name "] = name;
dRow[ "ext "] = ext;
dRow[ "writetime "] = wtime ;
dRow[ "size "] = Convert.ToString(size/1024)+ "K ";
dRow[ "content "] = content;

fileListTable.Rows.Add(dRow);
return;
}
private void fillfileListTable(Int32 pid, Int32 isFile, string fullname)

{
DataRow dRow = fileListTable.NewRow();

dRow[ "parentID "] = pid;
dRow[ "type "] = isFile;
dRow[