添加文件夹获得其树形结构,并构建其节点

定义文件信息

public class MyFileInfo
{
public string FileName { get; set; }//文件名
public string FilePath { get; set; }//文件路径
public long FileSize { get; set; }//文件大小
public string ParentPath { get; set; }//父路径
public string RelativePath { get; set; }//相对路径
public bool IsFile { get; set; }//是否为文件

//重写Equals方法,以便通过该值是否相等判断两个对象是否相等,在List中可以直接使用Contain方法确定是否已添加该项

public override bool Equals(object fileList)
{
if (this.FilePath.Equals(((CDBurnFileInfo)fileList).FilePath))
{
return true;
}
else
{
return false;
}
}
}

添加文件的方法

private void AddFile(TreeNode parentNode, string filePath)
{
MyFileInfo fl = new MyFileInfo();
fl.FilePath = filePath;
fl.FileName = Path.GetFileName(filePath);
fl.FileSize = new FileInfo(filePath).Length;
fl.IsFile = true;

if (!alFileList.Contains(fl))
{

TreeNode node = new TreeNode();
node.Text = fl.FileName;//节点显示名称

node.Name = fl.FilePath;
if (parentNode == null)
{
tvFileList.Nodes.Add(node);

}
else
{
node.Tag = parentNode.Name;//父节点path
parentNode.Nodes.Add(node);
fl.ParentPath = parentNode.Name;
}
alFileList.Add(fl);
}
}

//添加文件夹方法,通过递归将选择的文件夹下内容全部添加

private void AddFileOrDirectory(TreeNode parentNode,string filePath)
{
//parentPath
DirectoryInfo dir = new DirectoryInfo(filePath);
//不是目录 ,是文件
if (!dir.Exists)
{
AddFile(parentNode, filePath);
}
else
{
FileSystemInfo[] files = dir.GetFileSystemInfos();
//for (int i = 0; i < files.Length; i++)
MyFileInfo fl = new MyFileInfo();
TreeNode node = new TreeNode();
fl.FilePath = filePath;
fl.FileName = new DirectoryInfo(filePath).Name;
fl.IsFile = false;
if (!alFileList.Contains(fl))
{
node.Text = fl.FileName;
node.Tag = fl.FilePath;
node.Name = fl.FilePath;
if (parentNode == null)
{
tvFileList.Nodes.Add(node);
}
else
{
node.Tag = parentNode.Name;//父节点path
parentNode.Nodes.Add(node);
fl.ParentPath = parentNode.Name;
}
alFileList.Add(fl);
foreach (FileSystemInfo fsi in files)
{
FileInfo file = fsi as FileInfo;
//是文件
if (file != null)
{
AddFile(node, file.FullName);
}
//对于子目录,进行递归调用
else
AddFileOrDirectory(node, fsi.FullName);

}
}
}

}

//得到相对路径方法

string GetParentDir(CDBurnFileInfo fl)
{
string result = string.Empty;
if (fl.ParentPath != null)
{
CDBurnFileInfo cdbFileInfo= alFileList.Find(u => (u.FilePath == fl.ParentPath));
result = cdbFileInfo.FileName+"\\"+result;
result = GetParentDir(cdbFileInfo)+result;

}
return result;
}

在界面可以直接调用AddFileOrDirectory,传入文件或文件夹路径将其整个结构按树形结构Treeview构建完成。

构建完成后在,遍历文件列表,通过GetParentDir方法得到相对路径。

时间: 2024-08-07 00:13:41

添加文件夹获得其树形结构,并构建其节点的相关文章

vs解决方案中添加文件夹

一般我们在github上面看到的项目结构基本都是把项目放到src文件夹中,test放测试 查了半天也没查到这个是怎么产生的...这边只能用比较笨的方法来完成. 解决方法中是允许我们添加解决方案文件夹,它只是虚拟的,并不会添加一个文件夹到你的目录中,只是在*.sln中添加一个实现文件的标识.解决方案中会记录所有的文件信息. 我们添加文件夹的效果跟解决方案文件夹应该是类似的,然后再看看github上的*.sln,这样就没错. ①我们在解决方案目录中添加文件夹 ②然后手动添加到*.sln中 ③重新加载

Ant学习---第二节:Ant添加文件夹和文件夹集的使用

一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { public static void main(String[] args) { for(String arg : args) System.out.println("Hello World" + arg); } } 2.创建 build.xml 文件,代码如下: <?xml ver

【AFL(三)】afl-tmin修改:添加文件夹操作指令

前言: 之前分析tmin工具的时候,提到tmin的命令目前只能对文件进行操作,很多博客也提供了写脚本的方式对文件夹进行操作.本文是想通过修改tmin源代码的方式,实现添加新命令参数就可以对文件夹进行操作. 本文分为三部分:主要思路.部分实现细节.演示. 在文章最后给出了git地址,可以pull下来直接替换afl-tmin.c用. 主要思路: [一]分析源码 在main函数入口之前,有几个自变量.函数需要仔细查看: 1. 自变量 static u8 *in_file, /* Minimizer i

Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构

本文提供了一个在Windows环境下使用Visual Studio 2013编辑现有源代码并且保持目录结构的方法.本文使用VS2013中文社区版做示例(本版本为免费版,可在VS官网下载),其他版本的VS操作方式类似.打开VS2013,选择[菜单]-[新建]-[从现有代码创建项目]选择项目类型:Visual C++项目文件位置:创建完成后会在这个目录生成sln,suo,vcxproj,filters和user等VS解决方案和项目文件,这个必须和源代码的根目录保持一致,否则不能导入文件夹结构.项目名

怎么往mac中finder个人收藏里添加文件夹

1.打开Finder,点击左上角finder偏好设置 2.选择边栏 3.如果侧栏中没有的文件夹,直接长按文件夹直接拖入.

git 无法添加文件夹下文件

最近做项目时,发现无法提交某个子文件夹下的文件. google后发现可能是该子文件夹下有.git文件夹导致无法上传. 删除子文件夹下.git后,依然无法提交子文件夹下的文件. 继续google, 尝试以下方法: git rm --cached directory git add directory 注:directory为子文件夹的路径. 但是执行git rm --cached directory时,提示 fatal: Unable to create 'xx/.git/index.lock':

dos添加文件夹属性!

attrib 显示所有文件的属性:参数:+r或-r [文件名] 设置文件属性是否为只读 +h或-h [文件名] 设置文件属性是否隐含 +s或-s [文件名] 设置文件属性是否为系统文件 +a或-a [文件名] 设置文件属性是否为归档文件 /s 设置包括子目录的文件在内的文件属性 , 设置所有属性文件为归档文件

sqlserver查询指定树形结构的所有子节点

用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持): with subqry(id,name,pid) as ( select id,name,pid from test1 where id = 5 --指定id union all select test1.id,test1.name,test1.pid from test1,subqry where test1.pid = subqry.id)select* from subqry;

项目工具一:把文件夹里所有子文件夹与文件按树形结构导出

1.运行 cmd,进入dos界面. 2.输入命令(假设你要导出的文件夹是D盘的temp目录): cd d:\temp                              //进入要生成的文件夹目录 tree /f >>d:\tree.txt                 //保存当前文件夹内的树形结构到d:\tree.txt文件中 输入这三行命令就好了,然后D盘temp文件夹里面的所有文件夹名称和文件名就都出现在D盘根目录下的tree.txt文件中了. 示例: 卷 Project 的文