用c#做的一个目录树(可新建删除重命名)

Posted 那个远方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用c#做的一个目录树(可新建删除重命名)相关的知识,希望对你有一定的参考价值。

数据库表结构:

create table tree(
nd_id integer primary key not null,
nd_parent integer,
nd_text varchar(20)
)

 

 

 

代码文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 数据库_树结构
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //加载目录树
            gettreeview(); 
        }
        /// <summary>
        /// 向数据库传输数据
        /// </summary>
        /// <param name="str"></param>
        private void connect(string str)
        {
            string sqlcon = "Data Source = (local);Database = treedb; User ID =sa; PWD = ‘‘";
            SqlConnection sqlconn = new SqlConnection(sqlcon);
            sqlconn.Open();
            SqlCommand com = new SqlCommand(str, sqlconn);
            com.ExecuteNonQuery();
            sqlconn.Close();
        }
        /// <summary>
        /// 目录树加载
        /// </summary>
        private void gettreeview()
        {
            string constr = " Data Source = (local); DataBase = treedb; User ID =sa; PWD = ‘‘";
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            string sqlstr = " select * from tree";
            SqlDataAdapter sqldataadapter = new SqlDataAdapter(sqlstr, con);
            DataSet ds = new DataSet();
            sqldataadapter.Fill(ds, "tree");
            con.Close();

            // 加载根结点
            TreeNode tn = new TreeNode();
            tn.Text = "系统文件夹";
            tn.Name = "0";
            tn.Tag = "0";
            treeView1.Nodes.Add(tn);
            treeView1.SelectedNode = tn;

            // 加载子结点
            if (ds != null)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    tn = new TreeNode();
                    tn.Text = dr["nd_text"].ToString();
                    tn.Name = dr["nd_id"].ToString();
                    tn.Tag = dr["nd_parent"].ToString();
                    if (treeView1.SelectedNode != tn.Tag)
                    {
                        TreeNode[] tn_temp = treeView1.Nodes.Find(dr["nd_parent"].ToString(), true);
                        if (tn_temp.Length > 0)
                        {
                        treeView1.SelectedNode = tn_temp[0];
                        }
                    }
                    treeView1.SelectedNode.Nodes.Add(tn);
                }
            }
            //展开所有结点并且光标返回根结点
            treeView1.ExpandAll();
            treeView1.SelectedNode = treeView1.TopNode;
        }
        /// <summary>
        /// 添加结点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = new TreeNode();
            tn.Text = "新建文件夹";
            int i = Convert.ToInt32(treeView1.SelectedNode.Name);
            treeView1.SelectedNode.Nodes.Add(tn);

            string str1, str2;
            str1 = "declare @a integer,@b integer  set @a =(select max(nd_id)+1 from tree) set @b = " + i + "";
            str2 = "insert into tree(nd_id,nd_parent,nd_text) values (@a,@b,‘新建文件夹‘) ";
            connect(str1 + str2);
           
            treeView1.Nodes.Clear();
            gettreeview(); 
        }
        /// <summary>
        /// 删除结点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.IsExpanded == false)
            {
                int i = Convert.ToInt32(treeView1.SelectedNode.Name);
                treeView1.SelectedNode.Remove();

                string str1, str2;
                str1 = "declare @a integer set @a = " + i + "";
                str2 = "delete from tree where nd_id = @a";
                connect(str1 + str2);
            }
            else
            {
                MessageBox.Show("此文件夹非空,不能删除", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        /// <summary>
        /// 重命名结点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.LabelEdit = true;
            treeView1.SelectedNode.BeginEdit();
        }
        /// <summary>
        /// 结点重命名后触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            treeView1.SelectedNode.EndEdit(true);
            treeView1.LabelEdit = false;

            string nodename = e.Label;
            int i = Convert.ToInt32(treeView1.SelectedNode.Name);

            string str1, str2;
            str1 = "declare @a varchar(20) set @a = ‘" + nodename + "‘";
            str2 = " update tree set nd_text = @a where nd_id = " + i + "";
            connect(str1 + str2);
        }
    }
}
————————————————
版权声明:本文为CSDN博主「未知数007」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kinwa007/article/details/2801199

以上是关于用c#做的一个目录树(可新建删除重命名)的主要内容,如果未能解决你的问题,请参考以下文章

Git:重命名分支中的目录

创建和删除和拷贝和移动重命名

从 C# 重命名网页

bat 文件,它将重命名所有目录和文件

在 C# 中重命名目录 [关闭]

linux下文件夹的创建复制剪切重命名清空和删除命令