• 大小: 44KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2022-08-08
  • 语言: C#
  • 标签: treeview  

资源简介

C#遍历treeview节点,以及对应名字文件的内容获取 Q692791问题的回答,https://ask.csdn.net/questions/692791

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Q692791
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TreeNode tn = new TreeNode(Path.GetDirectoryName(dlg.SelectedPath));
                tn.Name = Path.GetDirectoryName(dlg.SelectedPath);
                treeView1.Nodes.Add(tn);
                LoadPath(dlg.SelectedPathtn);
            }
        }

        private void LoadPath(string p TreeNode tn)
        {
            DirectoryInfo di = new DirectoryInfo(p);
            foreach (var item in di.GetDirectories())
            {
                TreeNode tn1 = default(TreeNode);
                try
                {
                    tn1 = new TreeNode(Path.GetFileName(item.FullName));
                    tn.Nodes.Add(tn1);
                    LoadPath(item.FullName tn1);
                }
                catch
                {
                    if (tn1 != null)
                        tn.Nodes.Remove(tn1);
                }
                Application.DoEvents();
            }
            foreach (var txt in di.GetFiles(“*.txt“ SearchOption.TopDirectoryOnly))
            {
                TreeNode tn1 = new TreeNode(Path.GetFileName(txt.FullName));
                tn1.Tag = txt.FullName;
                tn.Nodes.Add(tn1);
            }
        }

        private void LoadText(StringBuilder sb TreeNode treeNode)
        {
            foreach (TreeNode item in treeNode.Nodes)
            {
                if (item.Tag == null)
                {
                    LoadText(sb item);
                }
                else
                {
                    try
                    {
                        if (item.Checked)
                        {
                            string s = File.ReadAllText((string)item.Tag);
                            sb.Append(s);
                            sb.Append(“\r\n“);
                        }
                    }
                    catch
                    {

                    }
                }
            }
        }

        private void treeView1_AfterCheck(object sender TreeViewEventArgs e)
        {
            if (e.Node.Checked)
            {
                foreach (TreeNode item in e.Node.Nodes) item.Checked = true;
            }
            StringBuilder sb = new StringBuilder();
            LoadText(sb treeView1.Nodes[0]);
            textBox1.Text = sb.ToString();
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         863  2018-06-23 00:04  Q692791\Q692791.sln
     文件       21504  2018-06-23 00:46  Q692791\Q692791.suo
     目录           0  2018-06-23 00:04  Q692791\Q692791\
     目录           0  2018-06-23 00:04  Q692791\Q692791\bin\
     目录           0  2018-06-23 00:12  Q692791\Q692791\bin\Debug\
     文件       10240  2018-06-23 00:40  Q692791\Q692791\bin\Debug\Q692791.exe
     文件       28160  2018-06-23 00:40  Q692791\Q692791\bin\Debug\Q692791.pdb
     文件       11600  2018-06-23 00:45  Q692791\Q692791\bin\Debug\Q692791.vshost.exe
     文件         490  2010-03-17 22:39  Q692791\Q692791\bin\Debug\Q692791.vshost.exe.manifest
     文件        2983  2018-06-23 00:40  Q692791\Q692791\Form1.cs
     文件        3279  2018-06-23 00:28  Q692791\Q692791\Form1.Designer.cs
     文件        5817  2018-06-23 00:28  Q692791\Q692791\Form1.resx
     目录           0  2018-06-23 00:04  Q692791\Q692791\obj\
     目录           0  2018-06-23 00:04  Q692791\Q692791\obj\x86\
     目录           0  2018-06-23 00:40  Q692791\Q692791\obj\x86\Debug\
     文件        2152  2018-06-23 00:04  Q692791\Q692791\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6386  2018-06-23 00:40  Q692791\Q692791\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         468  2018-06-23 00:28  Q692791\Q692791\obj\x86\Debug\GenerateResource.read.1.tlog
     文件        1034  2018-06-23 00:28  Q692791\Q692791\obj\x86\Debug\GenerateResource.write.1.tlog
     文件        1192  2018-06-23 00:45  Q692791\Q692791\obj\x86\Debug\Q692791.csproj.FileListAbsolute.txt
     文件       10240  2018-06-23 00:40  Q692791\Q692791\obj\x86\Debug\Q692791.exe
     文件         180  2018-06-23 00:28  Q692791\Q692791\obj\x86\Debug\Q692791.Form1.resources
     文件       28160  2018-06-23 00:40  Q692791\Q692791\obj\x86\Debug\Q692791.pdb
     文件         180  2018-06-23 00:12  Q692791\Q692791\obj\x86\Debug\Q692791.Properties.Resources.resources
     目录           0  2018-06-23 00:04  Q692791\Q692791\obj\x86\Debug\TempPE\
     文件         499  2018-06-23 00:04  Q692791\Q692791\Program.cs
     目录           0  2018-06-23 00:04  Q692791\Q692791\Properties\
     文件        1426  2018-06-23 00:04  Q692791\Q692791\Properties\AssemblyInfo.cs
     文件        2843  2018-06-23 00:04  Q692791\Q692791\Properties\Resources.Designer.cs
     文件        5612  2018-06-23 00:04  Q692791\Q692791\Properties\Resources.resx
     文件        1093  2018-06-23 00:04  Q692791\Q692791\Properties\Settings.Designer.cs
............此处省略2个文件信息

评论

共有 条评论