使用Windows Form 制作一个简易资源管理器
自制一个简易资源管理器----TreeView控件
第一步、新建project,进行基本设置;(Set as StartUp Project;View/Toolbox/TreeView)

第二步、开始添加节点
添加命名空间using System.IO;
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 System.IO; namespace _ResouceManager_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//这里是资源管理器的根路径
string strRoot = @"H:\自制资源管理器";//路径
CreateParent(strRoot);
} private void CreateParent(string strRoot)
{
//创建根节点parent
TreeNode parent = new TreeNode();
DirectoryInfo di=new DirectoryInfo(strRoot);
parent.Text= di.Name ;
parent.Tag = di.FullName; //添加父节点
tvResouceManager.Nodes.Add(parent); //创建子节点
CreateChild(strRoot,parent);
//展开所有节点
parent.ExpandAll(); } private void CreateChild(string path,TreeNode parent )
{
DirectoryInfo di = new DirectoryInfo(path);
//所有的子文件夹
DirectoryInfo[] dirs = di.GetDirectories();
//遍历子文件夹
foreach(DirectoryInfo dir in dirs)
{
//创建子节点
TreeNode child = new TreeNode();
child.Text = dir.Name;
//child.Tag = dir.FullName; //添加子节点
parent.Nodes.Add(child); //递归实现多级文件夹的遍历、创建子节点、添加子节点
CreateChild(dir.FullName,child); //添加文件节点
CreateFile(dir.FullName,child);
}
} private void CreateFile(string p, TreeNode child)
{
DirectoryInfo di = new DirectoryInfo(p);
//路径下的所有文件
FileInfo[] files = di.GetFiles();
//添加路径下的所有文件
foreach(FileInfo file in files)
{
//创建节点
TreeNode tn = new TreeNode();
tn.Text = file.Name;
// tn.Tag = file.FullName; //添加节点
child.Nodes.Add(tn);
}
} }
}

这里基本上完成了目录添加,尚不能增加文件、删除文件、移动文件等操作,还需继续努力。
右边是两个文本框,可以进行文本的编辑等(代码不全)。
首先在左边的treeview中点击某个节点,进行判断点击的是哪个节点,如果是.doc或者是.txt就可以编辑(其他文件类型如PDF之类可以自己写代码哦)。
先将上述代码中关于.Tag的注释取消
private void tvResouceManager_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag == null) return;
string path = e.Node.Tag.ToString();
if(path.LastIndexOf(".doc")>)
{
//如果点击的是.doc文档,将标题写入上文本框
txtTitle.Text = Path.GetFileNameWithoutExtension(e.Node.Text);
//文档内容写入下文本框,并使用指定的编码规则进行读文本操作
//txtContent.Text = File.ReadAllText(path,Encoding.GetEncoding("utf-8"));
txtContent.Text = File.ReadAllText(path, Encoding.Default);
} } private void btnSave_Click(object sender, EventArgs e)
{
if (tvResouceManager.SelectedNode == null) return;
if (tvResouceManager.SelectedNode.Tag == null) return; string path = tvResouceManager.SelectedNode.Tag.ToString(); if (path.LastIndexOf(".doc") > )
{
string content = txtContent.Text;
File.WriteAllText(path, content, Encoding.Default); MessageBox.Show("Save Successed"); }
}
使用Windows Form 制作一个简易资源管理器的更多相关文章
- 用XMLHttpRequest制作一个简易ajax
概述 jquery退出历史舞台之后,我们怎么来发送ajax请求呢?可以用相关的库,也可以自己制作一个简易的ajax. 需要说明的是,我们使用的是XMLHttpRequest 2,它几乎兼容所有主流浏览 ...
- 吴裕雄--天生自然python学习笔记:python 用pygame模块制作一个音效播放器
用 Sound 对象制作一个音效播放器. 应用程序总览 程序在执行后默认会把 WAV 音频文件加载到清单中,单击“播放”按钮可开始 播放,同时显示 “正在播放 xxx 音效”的信息 . 播放过程中,可 ...
- 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具
查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...
- (转)C#制作一个消息拦截器
首先,我们先要制作一个自定义Attribute,让他可以具有上下文读取功能,所以我们这个Attribute类要同时继承Attribute和IContextAttribute. 接口IContextAt ...
- C#制作一个消息拦截器(intercept)1
首先,我们先要制作一个自己定义Attribute,让他能够具有上下文读取功能,所以我们这个Attribute类要同一时候继承Attribute和IContextAttribute. 接口IContex ...
- Opencv探索之路(二十):制作一个简易手动图像配准工具
近日在做基于sift特征点的图像配准时遇到匹配失败的情况,失败的原因在于两幅图像分辨率相差有点大,而且这两幅图是不同时间段的同一场景的图片,所以基于sift点的匹配已经找不到匹配点了.然后老师叫我尝试 ...
- iOS:制作一个简易的计算器
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...
- QT制作一个图片播放器
前言:使用qt制作了一个简单的图片播放器,可以播放gif.png等格式图片 先来看看播放器的功能(当然是很简陋的,没有很深入的设计): 1.点击图片列表中图片进行播放. 2.自动播放,播放的图片的间隔 ...
- 使用jQuery制作一个简易的购物车结算流程
因为今天下午时候在网上买了东西,在结算界面的时候突发奇想的也想自己动手做一个结算界面,当然了,只是一个最简易的结算界面,有商品数量的加减,有单价和小计,单个多个删除,全选和区县全选等等一些小功能,我在 ...
随机推荐
- union内嵌struct用法
// union内嵌struct用法 // 众所周知,union为联合体,struct为结构体.下面根据实例谈谈用法 #include <stdio.h> #include & ...
- Sql获取周、月、年的首尾时间。
,) -- 本周周一 ,,,)) -- 本周周末 ,) -- 本月月初 ,,,)) -- 本月月末 ,,) -- 上月月初 ,,)) -- 上月月末 ,) -- 本年年初 ,,,)) -- 本年年末 ...
- 使用input range滑块,控制元素transform rotate旋转样式
<!DOCTYPE html> <html> <head> </head> <body> <!-- 第一步:设置div旋转对象和inp ...
- 正则表达式入门(六)匹配unicode和其他字符
匹配unicode字符有时候我们需要匹配ASCII范围之外的字符. "Qu'est-ce que la tolérance? c'est l'apanage de l'humanité. N ...
- c#基础3
Console.WriteLine("屏幕显示的内容"); Console.Write("屏幕显示的内容"); 两者区别是:Console.WriteLine( ...
- java selenium验证元素是否存在
public boolean ElementExist(WebDriver driver,By locator) { try { driver.findEle ...
- jmeter for循环嵌套if学习1
测试计划组成: vcondition的值都是tom. 执行结果:
- DEVExpress GridControl|TableView |FormatConditions 按一定格式设置相应内容
Get到一个新技能,感觉好棒.摘自DEV官网:https://www.devexpress.com/Support/Center/Example/Details/T135593 <Window ...
- DataTable数据导出CSV文件
public static void SaveAsExcel(DataTable dt1) { //System.Windows.Forms.SaveFileDialog sfd = new Syst ...
- DNS bind子域授权安装
失败经验:rhel 6.x bind 9.8,两台做子域授权,最后失败.原因不详. 改用rhel 5.5, bind 9.3,同样的配置,就成功了.具体记录一下9.3的配置. 安装:采用安装RHEL时 ...