资源简介
使用C#编写的获取某一文件夹下的目录树结构,使用递归的方法查找该文件夹下的所有文件夹和文件,并存到自定义类tree下

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
using System.Collections;
namespace 获取目录树
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
string path = Application.StartupPath + @“\“;
DirectoryInfo rootDir = new DirectoryInfo(path);
tree newtree = getTrees(rootDir);
}
long z = 1;//id计数
public class tree //自己定义一个类用于存储树结构
{
public long id;
public string text;//节点名字
public tree[] children;//子树
}
public tree getTrees(DirectoryInfo rootdir)
{
long temp = z;
tree rootTree = new tree();
rootTree.id = z;
rootTree.text = rootdir.Name;
DirectoryInfo[] firstchild = rootdir.GetDirectories();//获取文件夹下所有的子文件夹
FileInfo[] allFile = rootdir.GetFiles();//获取文件夹下所有的子文件
ArrayList newFile = new ArrayList();
for (int i = 0; i < allFile.Length; i++)//进行文件过滤
{
string name = allFile[i].Name;
if (name.Contains(“.dbf“) || name.Contains(“.prj“) || name.Contains(“.sbn“) || name.Contains(“.sbx“) || name.Contains(“shp.xml“) || name.Contains(“.shx“))//筛选文件类型
{
continue;
}
newFile.Add(allFile[i]);
}
rootTree.children = new tree[firstchild.Length + newFile.Count];//子树
for (int i = 0; i < firstchild.Length; i++)
{
tree sonTree = new tree();
z = Convert.ToInt64(temp.ToString() + “1“) + i;
sonTree = getTrees(firstchild[i]);//把文件夹下的文件夹拿来进行递归 新的子树
rootTree.children[i] = sonTree;
}
if (firstchild.Length == 0)
{
z = Convert.ToInt64(temp.ToString() + “1“);
}
//rootTree.children = new tree[allFile.Length];
for (int j = 0; j < newFile.Count; j++)
{
tree son = new tree();
FileInfo fi = ((FileInfo)newFile[j]);
son.text = fi.Name;
son.id = z + j + firstchild.Length;
rootTree.children[j + firstchild.Length] = son;
}
z = Convert.ToInt64(temp.ToString() + “1“);
return rootTree;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 187 2017-09-27 15:37 获取目录树\获取目录树\App.config
文件 654848 2017-03-20 20:02 获取目录树\获取目录树\bin\Debug\Newtonsoft.Json.dll
文件 0 2017-09-27 15:39 获取目录树\获取目录树\bin\Debug\文件夹1\新建文本文档.txt
文件 9216 2018-08-10 14:59 获取目录树\获取目录树\bin\Debug\获取目录树.exe
文件 187 2017-09-27 15:37 获取目录树\获取目录树\bin\Debug\获取目录树.exe.config
文件 26112 2018-08-10 14:59 获取目录树\获取目录树\bin\Debug\获取目录树.pdb
文件 22984 2018-08-10 14:59 获取目录树\获取目录树\bin\Debug\获取目录树.vshost.exe
文件 187 2017-09-27 15:37 获取目录树\获取目录树\bin\Debug\获取目录树.vshost.exe.config
文件 490 2018-04-12 07:35 获取目录树\获取目录树\bin\Debug\获取目录树.vshost.exe.manifest
文件 3010 2017-09-27 15:53 获取目录树\获取目录树\Form1.cs
文件 2008 2017-09-27 15:42 获取目录树\获取目录树\Form1.Designer.cs
文件 5817 2017-09-27 15:42 获取目录树\获取目录树\Form1.resx
文件 863 2017-09-27 15:37 获取目录树\获取目录树\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7064 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 0 2017-09-27 15:37 获取目录树\获取目录树\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2017-09-27 15:37 获取目录树\获取目录树\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2017-09-27 15:37 获取目录树\获取目录树\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 855 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\获取目录树.csproj.FileListAbsolute.txt
文件 975 2017-09-27 15:44 获取目录树\获取目录树\obj\Debug\获取目录树.csproj.GenerateResource.Cache
文件 10626 2017-09-27 15:44 获取目录树\获取目录树\obj\Debug\获取目录树.csprojResolveAssemblyReference.cache
文件 9216 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\获取目录树.exe
文件 180 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\获取目录树.Form1.resources
文件 26112 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\获取目录树.pdb
文件 180 2018-08-10 14:59 获取目录树\获取目录树\obj\Debug\获取目录树.Properties.Resources.resources
文件 527 2017-09-27 15:37 获取目录树\获取目录树\Program.cs
文件 1350 2017-09-27 15:37 获取目录树\获取目录树\Properties\AssemblyInfo.cs
文件 2882 2017-09-27 15:37 获取目录树\获取目录树\Properties\Resources.Designer.cs
文件 5612 2017-09-27 15:37 获取目录树\获取目录树\Properties\Resources.resx
文件 1102 2017-09-27 15:37 获取目录树\获取目录树\Properties\Settings.Designer.cs
文件 249 2017-09-27 15:37 获取目录树\获取目录树\Properties\Settings.settings
............此处省略15个文件信息
- 上一篇:flights.csv
- 下一篇:用C#实现的一个学生成绩管理系统
相关资源
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
评论
共有 条评论