如何显示所选树节点

时间:2017-07-21 15:22:28

标签: c# xml winforms treeview

我需要的是当选择树节点时,应用程序需要显示列出的所选文件夹(或文件)详细信息。详细信息应显示在同一窗口中,但与文件夹层次结构树分开显示。

我创建的应用程序以递归方式搜索文件/文件夹并写入稍后用于填充树视图的XML文件。我的问题是当我填充树视图时,它用xml(大小,创建日期,最后修改...)的属性填充它,我不希望它,我希望它只显示文件/文件夹的名称,当我单击树节点,它显示特定文件/文件夹的详细信息。

我使用此代码执行所有How to create and save an XML file containing complete hierarchy of files and folders for a specified folder?

但问题出在这些代码行中,它们在树视图中显示所有信息

           foreach (var att in xElement.Attributes())
           {
                treeNode.Text = treeNode.Text + " " + att.Name.LocalName + ":" + att.Value;
           }

任何人都可以帮我解决这个问题,如何在选择树节点时显示文件/文件夹的详细信息

这是我的全部代码

public partial class frmMain : Form
    {
        XDocument xDoc = null;
        string path = null;

        public frmMain()
        {
            InitializeComponent();
        }        
        /// <summary>
        /// When button Browse is clicked:
        /// - opens folder browser and lets user choose which folder to display
        /// - creates XML file based on selected path
        /// - fills treeView with that XML file
        /// </summary>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            listDetails.Items.Clear();
            FolderBrowserDialog fwd = new FolderBrowserDialog();
            if(fwd.ShowDialog() == DialogResult.OK)
            {
                lblPath.Text = "Path: " + fwd.SelectedPath; // Shows path selected by user 
                path = fwd.SelectedPath; // stores that path in string for later use
            }
            CreateXML(); 
            CreateTree();
        }
        /// <summary>
        /// Recursively search through files and folders to create XML file
        /// - 
        /// </summary>     
        private double Recursion(string folder, XElement folderElement)
        {
            folderElement.SetValue(folder);
            DirectoryInfo dirInfo = new DirectoryInfo(folder);

            int number_Of_Files = 0; // Number of files in folder
            double folder_Size = 0; // Folder size in bytes

            FileInfo[] files = null;
            //DirectoryInfo[] subDirs = null;

            try
            {
                files = dirInfo.GetFiles(); //Returns file list from current directory

                if (files != null)
                {
                    foreach (FileInfo f in files) //Lists all files in file list and 
                    {
                        double fileSize = f.Length;
                        folder_Size += fileSize;
                        folderElement.Add(new XElement("File", new object[] {
                        new XAttribute("Size",fileSize + "(bytes)"),
                        new XAttribute("Created", f.CreationTime.ToShortDateString()),
                        new XAttribute("LastAccess", f.LastAccessTime.ToShortDateString()),
                        new XAttribute("LastModified", f.LastWriteTime.ToShortDateString()),
                        f.Name

                    }));
                        number_Of_Files += 1; //Increments everytime it finds file in folder 
                    }
                }
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show(e.Message);
                writeErrorLog(e.Message);
            }

            catch (DirectoryNotFoundException e)
            {
                MessageBox.Show(e.Message);
                writeErrorLog(e.Message);
            }

            try
            {
                //Lists all sub folders in current directory
                foreach (string subDirs in Directory.GetDirectories(folder))
                {
                    XElement childDirectory = new XElement("Directory");
                    folderElement.Add(childDirectory);
                    double dirSize = Recursion(subDirs, childDirectory); //Call Recursion for found sub folder in folder 
                    folder_Size += dirSize;
                }
                // Writes attributes of XML folder element 
                folderElement.Add(new XAttribute[] {
                new XAttribute("FolderSize", folder_Size + "(bytes)"),
                new XAttribute("NumberOfFiles", number_Of_Files)
            });                
            }
            catch (UnauthorizedAccessException e)
            {
                MessageBox.Show(e.Message);
                writeErrorLog(e.Message);
            }

            catch (DirectoryNotFoundException e)
            {
                MessageBox.Show(e.Message);
                writeErrorLog(e.Message);
            }
            return folder_Size;
        }
        /// <summary>
        /// Creates tree node 
        /// -
        /// </summary>
        private void AddTreeNode(XElement xElement, TreeNode treeNode)
        {
            foreach (var att in xElement.Attributes())
            {
                treeNode.Text = treeNode.Text + " " + att.Name.LocalName + ":" + att.Value;
            }

            foreach (XElement childElement in xElement.Elements())
            {
                TreeNode Node = treeNode.Nodes[treeNode.Nodes.Add(new TreeNode(childElement.Value))];
                AddTreeNode(childElement, Node);
            }
        }
        /// <summary>
        /// Creates XML file 
        /// - If directory exists in path selected by user
        /// - sets root element for XML file
        /// - recursively search through files and folders in that path to create XML file
        /// </summary>
        private void CreateXML()
        {
            if (Directory.Exists(path))
            {
                string header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Directory></Directory> ";
                xDoc = XDocument.Parse(header);
                XElement root = xDoc.Root;

                Recursion(path, root);
            }
        }
        /// <summary>
        /// - Clears tree view everytime method is called 
        /// - Checks if XML document is null
        /// - If not creates tree node instance 
        /// - Uses method AddTreeNode to create tree nodes 
        /// - Fills tree view with those nodes
        /// </summary>
        private void CreateTree()
        {
            folderBrowserTree.Nodes.Clear();
            if(xDoc != null)
            {
                TreeNode rootNode = new TreeNode(xDoc.Root.FirstNode.ToString());
                AddTreeNode(xDoc.Root, rootNode);
                folderBrowserTree.Nodes.Add(rootNode);
                //folderBrowserTree.ExpandAll(); //If wanted tree view can display tree nodes as expanded
            }
        }
        /// <summary>
        /// When tree node is selected, displays tree node details in list view
        /// </summary>
        private void folderBrowserTree_AfterSelect(object sender, TreeViewEventArgs e)
        {            
            listDetails.Items.Clear(); // Clears list view everytime this method is called
            TreeDetails();
            string str = folderBrowserTree.SelectedNode.Text;

            var details = str.Split(); // Splits string by whitespaces
            listDetails.Items.Add("Name: " + details[0]);

            for (int i = 1; i < details.Length; i++)
            {
                listDetails.Items.Add(details[i]);
            }

        }

提前致谢

0 个答案:

没有答案