跳到正文

【C#进阶】如何实现一个简单的文件资料管理器源码分享

具体的实现源码如下,复制以下代码到你的项目工程下,并新建相应的Windows窗体模块,就可以跑起来了: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class FileManager : Form { public FileManager() { InitializeComponent(); } private void btnParent_Click(object sender, EventArgs e) { //添加根节点(TreeNodeCollection):当前树装菜单的集合 if (this.txtParent.Text != "") { this.treeView1.Nodes.Add(this.txtParent.Text); } } private void btnChild_Click(object sender, EventArgs e) { //添加子节点 //获得要添加子节点的根节点 TreeNode tn = this.treeView1.SelectedNode; //开始对选中的节点添加子节点 tn.Nodes.Add(this.txtChild.Text); } private void FileManager_Load(object sender, EventArgs e) { //开始载入系统的文件 string systemPath = "F:\\SystemPath"; //this.LoadDirectoryAndFile(systemPath, this.treeView1.Nodes); this.LoadFileAndDirectoryToNodes(systemPath, this.treeView1.Nodes); //更符合真实的情况 //加载我的电脑 //关键点:只需要找到本地磁盘CDEFG的节点 //this.treeView1.Nodes. //this.treeView1.Nodes.Find(""); foreach (TreeNode item in this.treeView1.Nodes) { //如果是我的电脑,就再次循环遍历 if (item.Text == "我的电脑") { foreach (TreeNode node in item.Nodes) { //MessageBox.Show(node.Text); /*if (node.Text == "本地磁盘C") { //MessageBox.Show("C Disk"+node.Text); string cDisk = "d:\\"; TreeNode tn = node; this.LoadFileAndDirectoryToNodes(cDisk, tn.Nodes); }*/ if (node.Text == "本地磁盘D") { string dDisk = "D:\\"; TreeNode tn = node; this.LoadFileAndDirectoryToNodes(dDisk, node.Nodes); } } } } } //把找到的文件和文件夹加载到节点上面 //加载文件夹和文件的第一种方法:(默认初始的文件路径就是根路径, 下面没有其他文件) private void LoadDirectoryAndFile(string path, TreeNodeCollection tc) { //this.LoadFileAndDirectoryToNodes(path, tc); //获得当前目录下面的所有文件路径(全路径)(这是第一层目录) string dics = Directory.GetDirectories(path); //得到文件名字 for (int i = 0; i < dics.Length; i++) { //从文件夹中的全路径中截取出来文件夹的名字(没有扩展名字) string dicName = Path.GetFileNameWithoutExtension(dics); //将文件夹的名字加载到节点集合下面 //tc是上一级的节点 //tn是当前级别的节点:{Text = "adt-bundle-windows-x86_64-20131030"} TreeNode tn = tc.Add(dicName); //获得节点(这是当前这一级的目录) //将这个文件夹下面的文件也加载到这个节点集合下面 string files = Directory.GetFiles(dics); //把文件夹下面的文件加载显示出来 for (int j = 0; j < files.Length; j++) { //根据文件路径获得文件名字的两种方法: //方法一: //tc.Add(files); //error i不等于j //int index = files.LastIndexOf('\\'); //files = files.Substring(index+1); //方法二: //files = Path.GetFileName(files); //然后把当前的文件也添加到我当前的文件节点 //tc.Add(files); //把当前文件添加到父节点 //tn.Nodes.Add(files); tn.Nodes.Add(Path.GetFileName(files)); //在这里把文件的全路径记录出来(tn是当前文件夹的节点) //tn.Tag = files; tn.Nodes.Tag = files; //有几个文件,就加上几个节点 } //开始获得第二层的目录 //newDics = Directory.GetDirectories(dics); //开始递归调用 //this.LoadDirectoryAndFile(Directory.GetDirectories(dics), tn.Nodes); error //这里的dics是我当前的文件夹,tn.Nodes是我刚刚添加的节点 //只需要把我当前的文件夹添加进去, 就会找到下一级的目录 LoadDirectoryAndFile(dics, tn.Nodes); } } //加载文件的第二种方法 private void LoadFileAndDirectoryToNodes(string filePath, TreeNodeCollection tc) { //1.首先获得文件路径下面的文件夹(全路径的文件夹) string dirs = Directory.GetDirectories(filePath); for (int i = 0; i < dirs.Length; i++) { //得到的仅仅是文件夹的名字 //dirs = Path.GetFileNameWithoutExtension(dirs); //把当前得我文件夹添加到节点 TreeNode tn = tc.Add(Path.GetFileNameWithoutExtension(dirs)); //tn就是当前的文件夹 //然后开始递归调用 this.LoadFileAndDirectoryToNodes(dirs, tn.Nodes); } //然后开始显示目录下面的文件 string fileNames = Directory.GetFiles(filePath); for (int i = 0; i < fileNames.Length; i++) { TreeNode tn = tc.Add(Path.GetFileName(fileNames)); //tc又变成了子节点了 tn.Tag = fileNames; //用于记录打开的所有文件 } } //双击打开文件的主要bug:1.在第一层目录中的文件不能打开; 2.只能打开文本类型的文件,其他文件类型不支持 private void treeView1_DoubleClick(object sender, EventArgs e) { //鼠标双击后显示文本(如果双击的是文件夹就不管) Object tn = this.treeView1.SelectedNode.Tag; string filePath = ""; if (tn != null) { filePath = tn.ToString(); } //只能支持指定的文件类型打开 string ext = Path.GetExtension(filePath); if (ext == ".txt" || ext == ".xml" || ext == ".html" || ext == ".log" || ext == ".ini") { //双击之后就开始显示文件(以文本形式读取文件) this.txtContent.Text = File.ReadAllText(filePath, Encoding.Default); } else if (ext == ".exe") { //来调用进程打开这个文件 //1.新建一个进程文件信息 ProcessStartInfo ps = new ProcessStartInfo(filePath); //2.创建一个进程实例对象 Process process = new Process(); //3.告诉进程要打开的文件信息 process.StartInfo = ps; //4.启动进程 process.Start(); } } } }

评论

填写昵称与邮箱即可评论,无需登录。

推荐阅读