如何用树形视图打开多个文件夹中的文件?

时间:2018-09-25 20:37:51

标签: c# treeview directory text-files

我最近开始编程,并且一直在设计我的第一个程序。它旨在在文件夹中存储和创建注释,但是在打开树形视图中第一个文件夹之外的其他文件夹中的txt文件时遇到了问题。我已经尝试解决了大约一个星期,但一直无法找到解决方案。我在编程中错过了什么?

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.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace InfoHub
{
    public partial class InfoHome : Form
    {
        public InfoHome()
        {
            InitializeComponent();
        }

        private void InfoHome_Load(object sender, EventArgs e)
        {

            //puts info on the top all windows
            this.TopMost = true;

        }
        //this string path makes it possiable for "C:\\Users\\epoch\\Desktop\\InfoHub"
        //to be used in button2_Click
        string path = "C:\\Users\\epoch\\Desktop\\InfoHub";
        private void button1_Click(object sender, EventArgs e)
        {
            ListDirectory(treeView1, "C:\\Users\\epoch\\Desktop\\InfoHub"); 
        }
            // this is the Directory Structure
            private void ListDirectory(TreeView treeView, string path)
            {
                treeView.Nodes.Clear();
                var rootDirectoryInfo = new DirectoryInfo(path);

                treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));

            }

            private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
            {
                var directoryNode = new TreeNode(directoryInfo.Name);
                foreach (var directory in directoryInfo.GetDirectories())
                    directoryNode.Nodes.Add(CreateDirectoryNode(directory));

                foreach (var file in directoryInfo.GetFiles())
                    directoryNode.Nodes.Add(new TreeNode(file.Name));

                return directoryNode;
            }

        //this button click opens programs
        private void button2_Click(object sender, EventArgs e)
        {
            string TreeNodeName = treeView1.SelectedNode.ToString().Replace("TreeNode: ",string.Empty);

            System.Diagnostics.Process.Start(path + "\\" + TreeNodeName);
        }

        //this opens the notpad 
        private void button3_Click(object sender, EventArgs e)
        {
            Process.Start("notepad.exe");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            folderCreator newform = new folderCreator();
            newform.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于节点名称是目录名称或文件名称,因此TreeNode.FullPath将代表文件的相对路径。您可以将其与基础path结合使用以获得完整路径:

private void button2_Click(object sender, EventArgs e)
{
    // "InfoHub" will appear twice below, GetParent removes one of them.
    var fullPath = Path.Combine(
        Directory.GetParent(path).FullName,
        treeView1.SelectedNode.FullPath
    );

    System.Diagnostics.Process.Start(fullPath);
}