TREEVIEW拖拽对应修改目录
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);
} |
|
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;
} |
|
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拖拽对应修改目录的更多相关文章
- C# TreeView 拖拽节点到另一个容器Panel中简单实现
C# TreeView 拖拽节点到另一个容器Panel中简单实现 用了这么久C#拖拽功能一直没有用到也就没用过,今天因为项目需要,领导特地给我简单讲解了下拖拽功能,真是的大师讲解一点通啊.特地写一篇博 ...
- MFC拖拽、选择目录、遍历文件
1.选择目录 void CDecryptFileDlg::OnBnClickedSel() { std::wstring selectedDir; WCHAR szDir[MAX_PATH]; Zer ...
- TreeView 拖拽 增删改
using Endv.Tools; using System; using System.Data; using System.Drawing; using System.IO; using Syst ...
- Qt之股票组件-自选股--列表可以拖拽、右键常用菜单
目录 一.开头嘴一嘴 二.效果展示 三.自选股列表 1.列表初始化 2.添加Item 3.右键菜单 4.拖拽Item 5.刷新数据 四.相关文章 原文链接:Qt之股票组件-自选股--列表可以拖拽.右键 ...
- ListView 多行拖拽排序
核心代码:修改ListView的属性,及绑定事件 // 初始化listView1. private void InitializeListView() { listView1.AllowDrop = ...
- Jquery 可拖拽的Ztree
比较懒,就只贴关键代码吧,自己把有用的属性全部打印出来了,也加了不少注释. 保存后涉及到的排序问题,刷新问题还未考虑到,后面有的话再加. $.fn.zTree.init($("#ztree& ...
- WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽
1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...
- Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)
今天再细研究了一下Treeview的用法,网上虽然总结了很多,但是还是有很多节点没有讲到了,也给使用中遇到很多问题.特地总结一下: 1.概念 Treeview用于显示按照树形结构进行组织的数据.Tre ...
- winform 两个TreeView间拖拽节点
/// <summary> /// 正在拖拽的节点 /// </summary> private TreeNode DragNode = null; /// <summa ...
随机推荐
- 工作流软件如何成为未来web的支柱
此文作者是 Kevin Lindquist,工作流平台Decisions的营销负责人,原文发表于VB上. Web 3.0 正在敲门,但是开门的人你永远都想不到:工作流软件. 传统上工作流软件是企业级的 ...
- but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)
以下是错误日志信息: 严重: The web application [/news] registered the JDBC driver [com.mysql.jdbc.Driver] but fa ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- SQL server 开启 cmdshell
GO RECONFIGURE GO GO RECONFIGURE GO EXEC master..xp_cmdshell 'net use Z: \\192.168.11.1\192.168.11.4 ...
- 机器学习的数学基础(1)--Dirichlet分布
机器学习的数学基础(1)--Dirichlet分布 这一系列(机器学习的数学基础)主要包括目前学习过程中回过头复习的基础数学知识的总结. 基础知识:conjugate priors共轭先验 共轭先验是 ...
- Sectong日志分析
http://tech.uc.cn/?p=2866#comments http://blog.sectong.com/blog/hw_bigdata.html
- SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace
1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...
- VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)
/************************************************************************/ /* 函数说明:判断有无Administrator ...
- 使用Spring AOP预处理Controller的参数
实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用,比如有这样一个Controller @Controller public class MatchOddsControll ...
- MySQL源码之Thread cache
MySQL server为每一个connection建立一个thread为其服务,虽然thread create比着fork process代价高,单高并发的情况下,也不可忽略. 所以增加了Threa ...