读取目录下所有目录和文件加载到TreeView

前段时间需要这个需求,于是乎 到网上查找到了资料.. 递归去读取... 将此代码贴在此处 以备用

 

代码
   
     
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
string id = HttpContext.Current.Request[ " id " ].ToString();
string path = System.Configuration.ConfigurationManager.AppSettings[ " UpPath " ].ToString() + id; // + "\\";
ViewState[ " path " ] = System.Configuration.ConfigurationManager.AppSettings[ " UpPath " ].ToString();
relateTreeView(TreeView1, path);
}
}

 

 

 

代码
   
     
public void relateTreeView(TreeView tv, string path)
{

tv.Nodes.Clear();
// 清空TreeView
tv.Nodes.Add( new TreeNode()); // 添加新节点
string [] pathinfo = Path.GetFullPath(path.Trim()).Split( char .Parse( " \\ " )); // 得到文件路径数组

#region // ////////最上层目录里面 文件的数量
DirectoryInfo di
= new DirectoryInfo(path);
int f = 0 ;
if (di.GetFiles( " *.* " ).Length > 0 )
{
f
= di.GetFiles( " *.* " ).Length;
foreach (var d in di.GetFiles( " *.* " ))
{
// f1 += d.Name + "</br>";
if (d.Name.Contains( " .db " ))
{
f
-- ;
}
}
}
#endregion

if (f != 0 )
{
tv.Nodes[
0 ].Text = " ( " + f + " ) " + pathinfo[pathinfo.Length - 1 ]; // 得到文件夹名
}
else
{
tv.Nodes[
0 ].Text = pathinfo[pathinfo.Length - 1 ];
}

tv.Nodes[
0 ].Value = pathinfo[pathinfo.Length - 1 ]; // path; // 得到文件夹的详细本地路径
tv.Nodes[ 0 ].Expanded = true ; // 展开根节点
tv.Nodes[ 0 ].ImageUrl = " images/directory.png " ; // 根节点图片
tv.Nodes[ 0 ].NavigateUrl = " ../PicView/PicView.html?DirPath= " + Server.UrlEncode(pathinfo[pathinfo.Length - 1 ] + " \\ " );
TraversingCatalog(tv.Nodes[
0 ], path); // 调用函数

}
代码
   
     
public void relateTreeView(TreeView tv, string path)
{

tv.Nodes.Clear();
// 清空TreeView
tv.Nodes.Add( new TreeNode()); // 添加新节点
string [] pathinfo = Path.GetFullPath(path.Trim()).Split( char .Parse( " \\ " )); // 得到文件路径数组

#region // ////////最上层目录里面 文件的数量
DirectoryInfo di
= new DirectoryInfo(path);
int f = 0 ;
if (di.GetFiles( " *.* " ).Length > 0 )
{
f
= di.GetFiles( " *.* " ).Length;
foreach (var d in di.GetFiles( " *.* " ))
{
// f1 += d.Name + "</br>";
if (d.Name.Contains( " .db " ))
{
f
-- ;
}
}
}
#endregion

if (f != 0 )
{
tv.Nodes[
0 ].Text = " ( " + f + " ) " + pathinfo[pathinfo.Length - 1 ]; // 得到文件夹名
}
else
{
tv.Nodes[
0 ].Text = pathinfo[pathinfo.Length - 1 ];
}

tv.Nodes[
0 ].Value = pathinfo[pathinfo.Length - 1 ]; // path; // 得到文件夹的详细本地路径
tv.Nodes[ 0 ].Expanded = true ; // 展开根节点
tv.Nodes[ 0 ].ImageUrl = " images/directory.png " ; // 根节点图片
tv.Nodes[ 0 ].NavigateUrl = " ../PicView/PicView.html?DirPath= " + Server.UrlEncode(pathinfo[pathinfo.Length - 1 ] + " \\ " );
TraversingCatalog(tv.Nodes[
0 ], path); // 调用函数

}
代码3
   
     
public bool TraversingCatalog(TreeNode tn, string path) // 遍历文件夹
{
string p = ViewState[ " path " ].ToString();
if (Directory.Exists(path) == false ) { return false ; }
DirectoryInfo dirInfo
= new DirectoryInfo(path);

int allNum = dirInfo.GetDirectories().Length + dirInfo.GetFiles( " *.* " ).Length; // 获取文件夹的子目录 + 其中包含点的文件
if (allNum == 0 ) // 没有任何文件夹和文件就建立"(空白)"节点并返回false
{
TreeNode empty
= new TreeNode();
empty.Text
= " (空白) " ; // 得到文件名
empty.Value = "" ; // 得到文件的详细本地路径
empty.ImageUrl = "" ; // 节点图片
// empty.Expanded = false; // 折叠节点
tn.ChildNodes.Add(empty); // 添加新节点
return false ;
}

// 循环文件夹(避免混乱,先循环文件夹)
int folderIndex = - 1 ; // 文件夹索引
foreach (DirectoryInfo folder in dirInfo.GetDirectories())
{

#region // 获取该文件夹下 文件的数量
int f = 0 ;
DirectoryInfo d1
= new DirectoryInfo(path + " \\ " + folder.Name);
if (d1.GetFiles( " *.* " ).Length > 0 )
{
f
= d1.GetFiles( " *.* " ).Length;
foreach (var d in d1.GetFiles( " *.* " ))
{
if (d.Name.Contains( " .db " ) || d.Name.Contains( " .ini " )) // Thumbs.db
{
f
-- ;
}
}
}
#endregion


folderIndex
++ ;
TreeNode folderNode
= new TreeNode();
if (f != 0 )
{
folderNode.Text
= " [ " + f.ToString() + " 张] " + folder.Name; // 得到文件夹名
}
else
{
folderNode.Text
= folder.Name;
}
folderNode.Value
= folder.FullName; // 得到文件夹的详细本地路径
folderNode.ToolTip = folder.Name; // 得到文件夹名
folderNode.Expanded = true ; // 折叠节点
folderNode.ImageUrl = " images/directory.png " ; // 节点图片

// if(folderNode.Parent.)
folderNode.NavigateUrl = " ../PicView/PicView.html?DirPath= " + Server.UrlEncode(tn.Value.Replace(p, "" ) + " \\ " + folder);

tn.ChildNodes.Add(folderNode);
// 添加新节点
TraversingCatalog(tn.ChildNodes[folderIndex], path + " / " + folder.Name); // 递归遍历其它文件夹
}


#region // //////////////便利 同级别 的 文件 ///////////禁用此段代码 因为只要读取文件夹就够了 不需要文件
// 循环文件

// foreach (FileInfo file in dirInfo.GetFiles("*.*")) // 循环扩展名为*.*的文件
// {
// TreeNode fileNode = new TreeNode();
// fileNode.Text = file.Name; // 得到文件名
// fileNode.Value = file.FullName; // 得到文件的详细本地路径
// fileNode.ToolTip = file.Name; // 得到文件名
// fileNode.Expanded = false; // 折叠节点
// fileNode.ImageUrl = "images/directory.png"; // 节点图片
// tn.ChildNodes.Add(fileNode); // 添加新节点
// }
#endregion


return true ;
}

 

最后 来张 效果图

读取目录下所有目录和文件加载到TreeView

你可能感兴趣的:(treeview)