#region treeview 绑定文件夹和文件
///
/// 根据文件夹绑定到树
///
///
///
///
public bool SetTreeNoByFilePath(TreeView treeview, string FilePath,ImageList imgs)
{
treeview.Nodes.Clear();
treeview.ImageList = imgs;
try
{
foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories())
{
TreeNode tn = new TreeNode(direc.Name);
tn.Text = direc.FullName;
SetTreeNodeIco(tn, "dir",imgs);
tn.Tag = direc.FullName;
SetSubDirectoryTreenode(direc, tn,imgs);
treeview.Nodes.Add(tn);
}
foreach (FileInfo finfo in new DirectoryInfo(FilePath).GetFiles())
{
TreeNode temptreenode = new TreeNode(finfo.Name);
temptreenode.Tag = finfo.FullName;
temptreenode.Text = finfo.Name;
SetTreeNodeIco(temptreenode, finfo.Extension, imgs);
treeview.Nodes.Add(temptreenode);
}
return true;
}
catch
{
return false;
}
}
///
/// 设置子目录的
///
/// 目录路径
///
///
private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn,ImageList imglist)
{
foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories())
{
TreeNode temptn = new TreeNode(dir.Name);
temptn.Tag = dir.FullName;
SetTreeNodeIco(temptn, "dir", imglist);
temptn.Text = dir.Name;
tn.Nodes.Add(temptn);
foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles())
{
TreeNode temptreenode = new TreeNode(fileinfo.Name);
temptreenode.Tag = fileinfo.FullName;
temptreenode.Text = fileinfo.Name;
SetTreeNodeIco(temptreenode, fileinfo.Extension,imglist);
temptn.Nodes.Add(temptreenode);
}
SetSubDirectoryTreenode(dir, temptn, imglist);
}
}
///
/// 为treeview设置小图标
///
///
///
///
private void SetTreeNodeIco(TreeNode tn, string strExt, ImageList imgs)
{
string ext = strExt.Replace(".", "");
if (ext.ToLower() == "dir")
{
tn.ImageIndex = imgs.Images.IndexOfKey("close");
tn.SelectedImageIndex = imgs.Images.IndexOfKey("open");
}
else if (ext.ToLower() == "doc" || ext.ToLower() == "rar" || ext.ToLower() == "txt")
{
tn.ImageIndex = imgs.Images.IndexOfKey(ext);
tn.SelectedImageIndex = imgs.Images.IndexOfKey(ext);
}
else
{
tn.ImageIndex = imgs.Images.IndexOfKey("other");
tn.SelectedImageIndex = imgs.Images.IndexOfKey("other");
}
}
#endregion
#region 只绑定文件夹
///
/// 根据文件夹绑定到树
///
///
///
///
public bool SetTreeNoByFilePath(TreeView treeview, string FilePath)
{
treeview.Nodes.Clear();
try
{
foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories())
{
TreeNode tn = new TreeNode(direc.Name);
tn.Text = direc.FullName;
tn.Tag = direc.FullName;
SetSubDirectoryTreenode(direc, tn);
treeview.Nodes.Add(tn);
}
return true;
}
catch
{
return false;
}
}
///
/// 设置子目录的
///
/// 目录路径
///
///
private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn)
{
foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories())
{
TreeNode temptn = new TreeNode(dir.Name);
temptn.Tag = dir.FullName;
temptn.Text = dir.Name;
tn.Nodes.Add(temptn);
foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles())
{
TreeNode temptreenode = new TreeNode(fileinfo.Name);
temptreenode.Tag = fileinfo.FullName;
temptreenode.Text = fileinfo.Name;
temptn.Nodes.Add(temptreenode);
}
SetSubDirectoryTreenode(dir, temptn);
}
}
#endregion