我有个文件夹,里面有多个文件夹和记事本文件,我现在要把他们读到treeview里面,右击treeview节点有 添加文件夹,添加文件,修改文件等菜单,单击treeview的文件夹节点展开,单击记事本文件就在一个文本框里显示出来。求个思路,有个小demo就更好了分不多了,大虾帮忙。

解决方案 »

  1.   

    这个并不复杂
    FileInfo 类操作文件 改名删除操作StreamReader 来读取txt 文件using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    string line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
      

  2.   


            /// <summary>
            /// 此方法映射磁盘
            /// </summary>
            public void reflectDriver()
            {
                DriveInfo[] dr = DriveInfo.GetDrives();
                string driveName = "";
                foreach (DriveInfo d in dr)
                {
                    switch (d.DriveType)
                    {
                        case DriveType.Fixed:
                            driveName = "本地磁盘(" + d.Name.Substring(0, 2) + ")";
                            break;
                        case DriveType.Removable:
                            driveName = "可移动磁盘(" + d.Name.Substring(0, 2) + ")";
                            break;
                        case DriveType.CDRom:
                            driveName = "DVD驱动器(" + d.Name.Substring(0, 2) + ")";
                            break;
                        case DriveType.Network:
                            driveName = "网络驱动器(" + d.Name.Substring(0, 2) + ")";
                            break;
                        default:
                            driveName = "未知(" + d.Name + ")";
                            break;
                    }
                    this.treeView1.Nodes.Add(d.Name, driveName);
                }
            }
    //调用
     private void button6_Click(object sender, EventArgs e)
            {
                reflectDriver();
            }
      

  3.   


            private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    if (e.Node.IsExpanded)
                    {
                        e.Node.Collapse();                }
                    else
                    {
                        e.Node.Expand();
                    }
                }
                else
                {
                    if (Directory.Exists(e.Node.Name))
                    {
                        try
                        {
                            string[] allDirectory = Directory.GetDirectories(e.Node.Name);
                            foreach (string s in allDirectory)
                            {
                                e.Node.Nodes.Add(s, s.Remove(0, s.LastIndexOf("\\") + 1));
                            }
                            listBox1.Items.Clear();
                            string[] allFiles = Directory.GetFiles(e.Node.Name);
                            foreach (string sf in allFiles)
                            {
                                //listBox1.Items.Add(sf.Remove(0, sf.LastIndexOf("\\") + 1));
                                listBox1.Items.Add(sf);
                                listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
                             }
                        }
                        catch
                        {
                        }
                    }
                    e.Node.Expand();
                }
            }        void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string path = listBox1.Items[1].ToString();
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));//读取文件            string str = null;
                while ((str = sr.ReadLine()) != null)//判断行
                {
                    listBox1.Items.Add(str);
                }
            }