1、更改樹節(jié)點(diǎn) /// <summary> /// 更改樹節(jié)點(diǎn) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tree_Solution_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { string selected_node_path_without_node_name = Regex.Match(selectTreeNodeFullPath(), @"[\S\s]*\\").Value; string full_path = null; full_path = Path.GetFullPath(selected_node_path_without_node_name + "\\" + e.Label); if (e.Label != null) { treeNodeNameChanged(full_path); addToRootPaths(full_path); if (e.Label.LastIndexOf(".") + 1 != 0) { string ext = e.Label.Substring(e.Label.LastIndexOf(".") + 1, (e.Label.Length - e.Label.LastIndexOf(".") - 1)); setTreeSolutionImageIndex(ext, tree_Solution.SelectedNode); } } } 2、樹節(jié)點(diǎn)重命名 /// <summary> /// 樹節(jié)點(diǎn)重命名 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeNodeNameChanged(string newNodePath) { try { FileInfo fileInfo = new FileInfo(selectTreeNodeFullPath()); fileInfo.MoveTo(newNodePath); } catch (Exception exception) { MessageBox.Show(exception.Message, "異常消息提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 3、獲取樹形目錄 /// <summary> /// 獲取樹形目錄 /// </summary> /// <param name="path"></param> public void getTreeViewData(string path) { TreeNode root = new TreeNode(); //root.Text = "MyTest"; getFiles(path, root); tree_Solution.Nodes.Add(root); if (root.Text == "site-packages") { root.ImageIndex = 2; root.SelectedImageIndex = 2; } else { root.ImageIndex = 4; root.SelectedImageIndex = 4; } this.KeyPreview = true; } /// <summary> /// 從樹形目錄中讀取文件,對(duì)應(yīng)生成樹形節(jié)點(diǎn) /// </summary> /// <param name="filePath"></param> /// <param name="node"></param> private void getFiles(string filePath, TreeNode node) { try { if (filePath == null || filePath == "") { } else { DirectoryInfo folder = new DirectoryInfo(filePath); node.Text = folder.Name; node.Tag = folder.FullName; node.ImageIndex = 5; FileInfo[] chldFiles = folder.GetFiles("*.*"); foreach (FileInfo chlFile in chldFiles) { if (!chlFile.Name.StartsWith(".") && chlFile.Name != "workspace" && chlFile.Name != "__pycache__" && !chlFile.Name.StartsWith( "__runner")) { TreeNode chldNode = new TreeNode(); chldNode.Text = chlFile.Name; chldNode.Tag = chlFile.FullName; string ext = chlFile.Name.Substring(chlFile.Name.LastIndexOf(".") + 1, (chlFile.Name.Length - chlFile.Name.LastIndexOf(".") - 1)); node.Nodes.Add(chldNode); setTreeSolutionImageIndex(ext,chldNode); } } DirectoryInfo[] chldFolders = folder.GetDirectories(); foreach (DirectoryInfo chldFolder in chldFolders) { if (!chldFolder.Name.StartsWith(".") && chldFolder.Name != "workspace" && chldFolder.Name != "__pycache__") { TreeNode chldNode = new TreeNode(); chldNode.Text = folder.Name; chldNode.Tag = folder.FullName; node.Nodes.Add(chldNode); getFiles(chldFolder.FullName, chldNode); } } } } catch(Exception exception) { MessageBox.Show(exception.Message, "異常消息提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
|
|