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. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  2. armv7a-mediatek451_001_vfp-linux-gnueabi-gcc: directory: No such file or directory 编译error

    release/vm_linux/output/hisense_android/mt5399_cn_android_JB/rel/obj/oss/source/arm_mali_ko/mali400- ...

  3. bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Sub ...

  4. http://jinnianshilongnian.iteye.com/blog/2018936

    http://jinnianshilongnian.iteye.com/blog/2018936

  5. ajax 初始化请求前携带参数

     $(function () {     function SetAjax(wxOpenId, departCode) {         $.ajaxSetup({             xhrF ...

  6. Process.StandardInput属性

    获取用于写入应用程序输入的流. 命名空间:System.Diagnostics程序集:System(在 system.dll 中) 语法     C# C++ VB   public StreamWr ...

  7. 一台机器上运行多个ActiveMq

    由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...

  8. 用C#中的params关键字实现方法形参个数可变

    个人认为,提供params关键字以实现方法形参个数可变是C#语法的一大优点.在方法形参列表中,数组类型的参数前加params关键字,通常可以在调用方法时代码更加精练. 例如,下面代码: class P ...

  9. POJ_2184_Cow_Exhibition_(动态规划,背包)

    描述 http://poj.org/problem?id=2184 n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大. Cow Exhibi ...

  10. [ZOJ 3631] Watashi's BG

    Watashi's BG Time Limit: 3 Seconds      Memory Limit: 65536 KB Watashi is the couch of ZJU-ICPC Team ...