Esempio n. 1
0
        /// <summary>
        /// 保存节点名修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dirView_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            //            bool isDir = e.Node.Nodes.Count != 0;
            if (dirView.SelectedNode != null)
            {
                bool isDir = TreeNodeHelper.Get <string>(dirView.SelectedNode, "isDir") == "1";

                if (isDir)
                {
                    textBox1.Enabled = false;
                    return;
                }
                else
                {
                    textBox1.Enabled = true;
                    SelectedNode     = e.Node;
                    //this.textBox1.Text = e.Node.Tag == null ? "" : e.Node.Tag.ToString();

                    textBox1.Text = TreeNodeHelper.Get <string>(e.Node, "template");

                    Console.WriteLine(1);
                }
            }


            if (EditingNode == null)
            {
                return;
            }
            else
            {
                EditingNode.EndEdit(false);
                EditingNode = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 递归生成treeView的json数据
        /// </summary>
        /// <param name="tr"></param>
        /// <returns></returns>
        public List <dynamic> AddChildren(TreeNode tr)
        {
            List <dynamic> list = new List <dynamic>();

            if (tr.Nodes.Count > 0)
            {
                foreach (TreeNode treeNode in tr.Nodes)
                {
                    if (treeNode.Nodes.Count > 0)
                    {
                        List <dynamic> children = AddChildren(treeNode);
                        dynamic        item     = new { children = children, name = treeNode.Text, isDir = "1" };
                        list.Add(item);
                    }
                    else
                    {
                        //if (string.IsNullOrEmpty(treeNode.Tag?.ToString()))
                        if (string.IsNullOrEmpty(TreeNodeHelper.Get <string>(treeNode, "template")))
                        {
                            if (TreeNodeHelper.Get <string>(treeNode, "isDir") == "1")
                            {
                                list.Add(new { name = treeNode.Text, isDir = "1" });
                            }
                            else
                            {
                                list.Add(new { name = treeNode.Text });
                            }
                        }
                        else
                        {
                            //list.Add(new {name = treeNode.Text, template = treeNode.Tag.ToString()});
                            list.Add(new
                            {
                                name     = treeNode.Text,
                                template = TreeNodeHelper.Get <string>(treeNode, "template"),
                                isDir    = "0"
                            });
                        }
                    }
                }
            }
            else
            {
                list.Add(new { name = tr.Text });
            }

            return(list);
        }
Esempio n. 3
0
        /// <summary>
        /// 在硬盘上创建文件及文件夹
        /// </summary>
        /// <param name="node"></param>
        /// <param name="pathList"></param>
        protected void CreateFiles(TreeNode node, List <string> pathList, string path)
        {
            string parentPath = Path.Combine(Path.GetFullPath(path), string.Join("/", pathList.ToArray()));

            if (node.Nodes.Count > 0)
            {
                var replaceedToken = ReplaceToken(node.Text);
                Directory.CreateDirectory(Path.Combine(parentPath, replaceedToken));
                pathList.Add(replaceedToken);
                foreach (TreeNode treeNode in node.Nodes)
                {
                    List <string> list = DeepCopyByBin(pathList);
                    CreateFiles(treeNode, list, path);
                }
            }

            else
            {
                if (node.Text.Split('.').Length > 1)
                {
                    //File.WriteAllText(Path.Combine(parentPath, node.Text), ReplaceToken(node.Tag?.ToString()));
                    File.WriteAllText(Path.Combine(parentPath, ReplaceToken(node.Text)),
                                      ReplaceToken(TreeNodeHelper.Get <string>(node, "template")));
                }
                else
                {
                    if (TreeNodeHelper.Get <string>(node, "isDir") == "1")
                    {
                        Directory.CreateDirectory(Path.Combine(parentPath, ReplaceToken(node.Text)));
                        //ReplaceToken(node.Tag?.ToString()));
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(FileExt))
                        {
                            File.WriteAllText(Path.Combine(parentPath, $"{ReplaceToken(node.Text)}.{FileExt}"),
                                              //ReplaceToken(node.Tag?.ToString()));
                                              ReplaceToken(TreeNodeHelper.Get <string>(node, "template")));
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        protected void createDirOrFile(bool isDir)
        {
            if (dirView.SelectedNode == null)
            {
                Tip tip = new Tip();
                tip.Show();
            }
            else if (TreeNodeHelper.Get <string>(dirView.SelectedNode, "isDir") == "0")
            {
                Tip tip = new Tip("请选择文件夹");
                tip.Show();
            }
            else
            {
                bool isFirst = true;
                dirView.SelectedNode.Expand();
                int i = 0;
                while (true)
                {
                    bool b = false;
                    foreach (TreeNode node in dirView.SelectedNode.Nodes)
                    {
                        if (isDir)
                        {
                            b = node.Text.Equals(isFirst ? $"新建文件夹" : $"新建文件夹({i})");
                        }
                        else
                        {
                            b = node.Text.Equals(isFirst ? $"新建文件" : $"新建文件({i})");
                        }

                        if (b)
                        {
                            break;
                        }
                    }

                    if (b)
                    {
                        isFirst = false;
                        i++;
                    }
                    else
                    {
                        if (isDir)
                        {
                            TreeNode newNode = isFirst
                                ? new TreeNode()
                            {
                                Text               = $"新建文件夹",
                                ImageIndex         = 0,
                                SelectedImageIndex = 0
                            }
                                : new TreeNode()
                            {
                                Text               = $"新建文件夹({i})",
                                ImageIndex         = 0,
                                SelectedImageIndex = 0
                            };
                            newNode.BeginEdit();

                            TreeNodeHelper.Edit(newNode, "isDir", "1");

                            dirView.SelectedNode.Nodes.Add(newNode);
                            dirView.SelectedNode = newNode;
                            newNode.BeginEdit();
                        }
                        else
                        {
                            TreeNode newNode = isFirst
                                ? new TreeNode()
                            {
                                Text               = $"新建文件",
                                ImageIndex         = 1,
                                SelectedImageIndex = 1
                            }
                                : new TreeNode()
                            {
                                Text               = $"新建文件({i})",
                                ImageIndex         = 1,
                                SelectedImageIndex = 1
                            };
                            newNode.BeginEdit();

                            TreeNodeHelper.Edit(newNode, "isDir", "0");

                            dirView.SelectedNode.Nodes.Add(newNode);
                            dirView.SelectedNode = newNode;
                            newNode.BeginEdit();
                        }

                        break;
                    }
                }
            }
        }