TREEVIEW拖拽对应修改目录

 
 
 
using System.IO;
 
 
        private static string RootPath = @"D:\Administrator\Documents\TestData";
        //返回 D:\Administrator\Documents
        private string myPath = GetDirectoryParentPath(RootPath); 
        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.AllowDrop = true;
            treeView1.HideSelection = false;
            DirectoryToTreeNode(RootPath,treeView1);

}

 
 
//添加目录到TREEVIEW
       private void DirectoryToTreeNode(string RootDir, TreeView treeView)
        {
            treeView.Nodes.Clear();
            DirectoryInfo di = new DirectoryInfo(RootDir);
            TreeNode rootNode = new TreeNode(di.Name);
            GetDirs(di.GetDirectories(), rootNode);
            treeView.Nodes.Add(rootNode);
            rootNode.Expand();

}

 
       private void GetDirs(DirectoryInfo[] subDirs, TreeNode treeNode)
        {
            foreach (DirectoryInfo d in subDirs)
            {
                TreeNode node = new TreeNode(d.Name, 0, 0);
                DirectoryInfo[] subSubDirs = d.GetDirectories();
                GetDirs(subSubDirs, node);
                treeNode.Nodes.Add(node);
            }

}

 
 
 
   /*问题
            C:\Program Files\\\
            C:\Program Files///
         */
        private bool DirectoryExists(string path)
        {
            DirectoryInfo d = new DirectoryInfo(path);
            return d.Exists;

}

 
       //目录 D:\Administrator\Documents\TestData
        //返回 D:\Administrator\Documents
        private static string GetDirectoryParentPath(string path)
        {
            DirectoryInfo d = new DirectoryInfo(path);
            return d.Parent.FullName;

}

 
        //目录 D:\Administrator\Documents\TestData
        //返回 TestData
        private string ExtractDirectoryName(string path)
        {
            DirectoryInfo d = new DirectoryInfo(path);
            return d.Name;

}

 
//移动文件夹
     //C:\Windows\a
    //C:\Windows\b
    //实际运行过程 Directory.Move( "C:\Windows\a" , "C:\Windows\b\a");
    private bool MoveDirectory(string sourceDirName, string destDirName)
    {
        destDirName = String.Format("{0}\\{1}", destDirName, ExtractDirectoryName(sourceDirName));
        //源目录存在 但目标目录不存在
        if (DirectoryExists(sourceDirName) && !DirectoryExists(destDirName)) 
        {
            Directory.Move(sourceDirName, destDirName);
            //源目录不存在 但目标目录存在
            if (!DirectoryExists(sourceDirName) && DirectoryExists(destDirName)) 
                return true;
        }
        else
            return false;
        return false;

}

 
 
 
 TreeView拖拽操作
       private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            TreeNode node2 = treeView1.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
            //节点2不存在
            //节点1 = 节点2
            //节点1 往它的父一级节点拖拽
            if ((node2 == null) ||(node1 == node2)|| (node1.Parent == node2))
            {
                treeView1.SelectedNode = node1;
                SetTreeNodeColorDefault();
                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;
                treeView1.SelectedNode = node2;
                SetTreeNodeColorBlue();
 
                //当一个父节点往它的子节点中拖拽时
                while (node2.Parent != null)
                {
                    if (node2.Parent == node1)
                    {
                        e.Effect = DragDropEffects.None;
                        return;
                    }
                    node2 = node2.Parent;
                }
            }

}

 
        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            //拖拽的节点指向目标的 这个目标节点
            TreeNode node2 = treeView1.GetNodeAt( treeView1.PointToClient(new Point(e.X, e.Y)) );
            if (node1 != node2)
            {
                if (node1.Parent != node2)
                {
                    string dir1 = myPath + "\\" + node1.FullPath;
                    string dir2 = myPath + "\\" + node2.FullPath;
 
                    textBox1.Text = dir1;
                    textBox2.Text = dir2;
                  
                    if (MoveDirectory(textBox1.Text, textBox2.Text))
                    {
                    //MessageBox.Show("Finished");
 
                        // Remove drag node from parent
                        if (node1.Parent == null)
                            treeView1.Nodes.Remove(node1);
                        else
                            node1.Parent.Nodes.Remove(node1);
                        node2.Nodes.Add(node1);
                        treeView1.SelectedNode = node1;
                        node2.Expand();
                    }
                    
                }
            }

}

 
 设置节点颜色

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
 
            SetTreeNodeColorDefault();

}

 
        private void SetTreeNodeColorBlue()
        {
            if (node1 != null)
            {
                node1.BackColor = Color.FromArgb(51, 153, 255);//蓝色 
                node1.ForeColor = Color.White;
            }
        }
 
        private void SetTreeNodeColorDefault()
        {
            if (node1 != null)
            {
                node1.BackColor = SystemColors.Window;
                node1.ForeColor = Color.Black;
            }

}

 

附件列表

TREEVIEW拖拽对应修改目录的更多相关文章

  1. C# TreeView 拖拽节点到另一个容器Panel中简单实现

    C# TreeView 拖拽节点到另一个容器Panel中简单实现 用了这么久C#拖拽功能一直没有用到也就没用过,今天因为项目需要,领导特地给我简单讲解了下拖拽功能,真是的大师讲解一点通啊.特地写一篇博 ...

  2. MFC拖拽、选择目录、遍历文件

    1.选择目录 void CDecryptFileDlg::OnBnClickedSel() { std::wstring selectedDir; WCHAR szDir[MAX_PATH]; Zer ...

  3. TreeView 拖拽 增删改

    using Endv.Tools; using System; using System.Data; using System.Drawing; using System.IO; using Syst ...

  4. Qt之股票组件-自选股--列表可以拖拽、右键常用菜单

    目录 一.开头嘴一嘴 二.效果展示 三.自选股列表 1.列表初始化 2.添加Item 3.右键菜单 4.拖拽Item 5.刷新数据 四.相关文章 原文链接:Qt之股票组件-自选股--列表可以拖拽.右键 ...

  5. ListView 多行拖拽排序

    核心代码:修改ListView的属性,及绑定事件 // 初始化listView1. private void InitializeListView() { listView1.AllowDrop = ...

  6. Jquery 可拖拽的Ztree

    比较懒,就只贴关键代码吧,自己把有用的属性全部打印出来了,也加了不少注释. 保存后涉及到的排序问题,刷新问题还未考虑到,后面有的话再加. $.fn.zTree.init($("#ztree& ...

  7. WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽

    1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...

  8. Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)

    今天再细研究了一下Treeview的用法,网上虽然总结了很多,但是还是有很多节点没有讲到了,也给使用中遇到很多问题.特地总结一下: 1.概念 Treeview用于显示按照树形结构进行组织的数据.Tre ...

  9. winform 两个TreeView间拖拽节点

    /// <summary> /// 正在拖拽的节点 /// </summary> private TreeNode DragNode = null; /// <summa ...

随机推荐

  1. C++ 11 笔记 (四) : std::bind

    std::bind 接受一个可调用的对象,一般就是函数呗.. 还是先上代码: void func(int x, int y, int z) { std::cout << "hel ...

  2. 关于OA中权限越级的问题

    最近被人问了一个问题, 在OA中我, 经理出差了,下属需要用到 经理的权限,应该怎么处理. 这个问题比较简单,大神,请指点一下. 一开始 ,我就被搞懵了. 我的回答是: 经理出差之前赋给权限就可以了. ...

  3. Net Core Docker

    Net Core Docker轻量级的web框架   .net core现在已经有了大的发展,虽然笔者现在已经从事python开发,但是一直在关注.net的发展,在逛博客园的时候,发现有大家都会提到N ...

  4. iOS9 升级设置

    今天升级了iOS9, Xcode7.1 ; 打开之前的工程发现网络请求出错了, 参照UM开发文档, 对info.plist进行了配置如下: 1. 以iOS9 SDK编译的工程会默认以SSL安全协议进行 ...

  5. Ubuntu/Linux下7款轻量级编辑器 (转)

    From http://www.feiyan.info/39.html 在Windows卧铺使用Zend Studio或者EditPlus写PHP,Zend Studio适合大项目,EditPlus配 ...

  6. Django下TemplateDoesNotExist 异常的解决方法:

    在settings中添加代码如下获取templates路径: import os import os.path BASE_DIR = os.path.dirname(os.path.dirname(_ ...

  7. bzoj 3160: 万径人踪灭 manachar + FFT

    3160: 万径人踪灭 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 133  Solved: 80[Submit][Status][Discuss] ...

  8. Android学习之Image操作及时间日期选择器

    一.基础学习 1.ImageView是图片容器,就相当于RadioGroup是RadioButton的容器一样,是View的直接子类. 1: <ImageView 2: android:id=& ...

  9. linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'

    linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...

  10. 17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 得到复制master binary log 位置:

    17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 得到复制master binary log 位置: 你需要master ...