• 资料管理


    过程

    添加一个TreeView控件:

    窗体加载的时候把文件添加到树;

    View Code
     1         private void Form1_Load(object sender, EventArgs e)
     2         {
     3             CreateParentNode();
     4         }
     5 
     6         void CreateParentNode()
     7         {
     8             tvType.Nodes.Clear();
     9             string path = "资料";
    10             TreeNode parent = new TreeNode();
    11             parent.Text = path;
    12             tvType.Nodes.Add(parent);
    13             CreateChildNode(path, parent);
    14         }
    15 
    16         void CreateChildNode(string path,TreeNode parent)
    17         {
    18             DirectoryInfo di = new DirectoryInfo(path);
    19             DirectoryInfo[] dis = di.GetDirectories();
    20             foreach (DirectoryInfo item in dis)
    21             {
    22                 TreeNode child = new TreeNode();
    23                 child.Text = item.Name;
    24                 child.Tag = item.FullName;
    25                 parent.Nodes.Add(child);
    26                 CreateFileNode(item.FullName,child);
    27             }
    28         }
    29         void CreateFileNode(string path, TreeNode child)
    30         {
    31             DirectoryInfo di = new DirectoryInfo(path);
    32             FileInfo[] fis = di.GetFiles();
    33             foreach (FileInfo  item in fis)
    34             {
    35                 TreeNode tn = new TreeNode();
    36                 tn.Text = item.Name;
    37                 tn.Tag = item.FullName;
    38                 child.Nodes.Add(tn);
    39             }
    40         }

    选中文件的时候把文件的内容加到文本框上

    View Code
            private void tvType_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (e.Node.Level == 2)
                {
                    if (!e.Node.Text.ToString().Contains(".txt")) return;
                    
                        txtContens.Text = File.ReadAllText(e.Node.Tag.ToString(), Encoding.GetEncoding("gb2312"));
                        txtTitle.Text = Path.GetFileNameWithoutExtension(e.Node.Text);
                    
                }
            }

    添加一个保存按钮

    View Code
            private void btnSave_Click(object sender, EventArgs e)
            {
                if (tvType.SelectedNode == null)
                    return;
                if (tvType.SelectedNode.Tag == null)
                    return;
                string path = tvType.SelectedNode.Tag.ToString();
                if (path.LastIndexOf(".txt") > 0)
                {
                    string content = txtContens.Text;
                    File.WriteAllText(path,content,Encoding.GetEncoding("gb2312"));
                    MessageBox.Show("保存成功");
                    CreateParentNode();
                }
            }
  • 相关阅读:
    .netCore读取配置文件
    初识.netCore以及如何vs2019创建项目和发布
    深度解析.NetFrameWork/CLR/C# 以及C#6/C#7新语法
    Asp.Net六大内置对象
    MVC的View本质和扩展
    Asp.net管道模型之(HttpModules 和 HttpHandler)
    Serf:Gossip Protocol
    Consul:ANTI-ENTROPY
    Consul:网络坐标
    Consul:Gossip协议
  • 原文地址:https://www.cnblogs.com/hejinyang/p/2809991.html
Copyright © 2020-2023  润新知