zoukankan      html  css  js  c++  java
  • 使用Windows Form 制作一个简易资源管理器

    自制一个简易资源管理器----TreeView控件

      第一步、新建project,进行基本设置;(Set as StartUp Project;View/Toolbox/TreeView)

      

        第二步、开始添加节点

      添加命名空间using System.IO;    

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using System.IO;
    11 
    12 namespace _ResouceManager_
    13 {
    14     public partial class Form1 : Form
    15     {
    16         public Form1()
    17         {
    18             InitializeComponent();
    19         }
    20 
    21         private void Form1_Load(object sender, EventArgs e)
    22         {
    23             //这里是资源管理器的根路径
    24             string strRoot = @"H:自制资源管理器";//路径
    25             CreateParent(strRoot);
    26         }
    27 
    28         private void CreateParent(string strRoot)
    29         {
    30             //创建根节点parent
    31             TreeNode parent = new TreeNode();
    32             DirectoryInfo di=new DirectoryInfo(strRoot);
    33             parent.Text= di.Name ;
    34             parent.Tag = di.FullName;
    35 
    36             //添加父节点
    37             tvResouceManager.Nodes.Add(parent);
    38 
    39             //创建子节点
    40             CreateChild(strRoot,parent);
    41             //展开所有节点
    42             parent.ExpandAll();
    43 
    44         }
    45 
    46         private void CreateChild(string path,TreeNode parent )
    47         {
    48             DirectoryInfo di = new DirectoryInfo(path);
    49             //所有的子文件夹
    50             DirectoryInfo[] dirs = di.GetDirectories();
    51             //遍历子文件夹
    52             foreach(DirectoryInfo dir in dirs)
    53             {
    54                 //创建子节点
    55                 TreeNode child = new TreeNode();
    56                 child.Text = dir.Name;
    57                 //child.Tag = dir.FullName;
    58 
    59                 //添加子节点
    60                 parent.Nodes.Add(child);
    61 
    62                 //递归实现多级文件夹的遍历、创建子节点、添加子节点
    63                 CreateChild(dir.FullName,child);
    64 
    65                 //添加文件节点
    66                 CreateFile(dir.FullName,child);                
    67             }
    68         }
    69 
    70         private void CreateFile(string p, TreeNode child)
    71         {
    72             DirectoryInfo di = new DirectoryInfo(p);
    73             //路径下的所有文件
    74             FileInfo[] files = di.GetFiles();
    75             //添加路径下的所有文件
    76             foreach(FileInfo file in files)
    77             {
    78                 //创建节点
    79                 TreeNode tn = new TreeNode();
    80                 tn.Text = file.Name;
    81                // tn.Tag = file.FullName;
    82 
    83                 //添加节点
    84                 child.Nodes.Add(tn);
    85             }
    86         }
    87 
    88     }
    89 }
    View Code

     

      这里基本上完成了目录添加,尚不能增加文件、删除文件、移动文件等操作,还需继续努力。

      右边是两个文本框,可以进行文本的编辑等(代码不全)。

      首先在左边的treeview中点击某个节点,进行判断点击的是哪个节点,如果是.doc或者是.txt就可以编辑(其他文件类型如PDF之类可以自己写代码哦)。

      先将上述代码中关于.Tag的注释取消

      

     1   private void tvResouceManager_AfterSelect(object sender, TreeViewEventArgs e)
     2         {
     3             if (e.Node.Tag == null) return;
     4             string path = e.Node.Tag.ToString();
     5             if(path.LastIndexOf(".doc")>0)
     6             {
     7                 //如果点击的是.doc文档,将标题写入上文本框
     8                 txtTitle.Text = Path.GetFileNameWithoutExtension(e.Node.Text);
     9                 //文档内容写入下文本框,并使用指定的编码规则进行读文本操作
    10                 //txtContent.Text = File.ReadAllText(path,Encoding.GetEncoding("utf-8"));
    11                 txtContent.Text = File.ReadAllText(path, Encoding.Default);
    12             }
    13 
    14         }
    15 
    16         private void btnSave_Click(object sender, EventArgs e)
    17         {
    18             if (tvResouceManager.SelectedNode == null) return;
    19             if (tvResouceManager.SelectedNode.Tag == null) return;
    20 
    21             string path = tvResouceManager.SelectedNode.Tag.ToString();
    22 
    23             if (path.LastIndexOf(".doc") > 0)
    24             {
    25                 string content = txtContent.Text;
    26                 File.WriteAllText(path, content, Encoding.Default);
    27 
    28                 MessageBox.Show("Save Successed");
    29               
    30             }
    31         }
    View Code

      

  • 相关阅读:
    git 提交
    Git提交出现 Everything up-to-date
    vscode 记住git用户密码
    git 将本地项目关联到远程仓库
    ubuntu19.04 安装workbench
    修改vscode终端样式
    python3练习
    C#实现MJPEG服务器
    在WPF中使用AForge控件
    FtpWebRequest.UsePassive属性:设置FTP工作模式
  • 原文地址:https://www.cnblogs.com/5696-an/p/4999300.html
Copyright © 2011-2022 走看看