C#文件文件夹拖拽功能的实现
来源:互联网 发布:淘宝网积分怎么用 编辑:程序博客网 时间:2024/06/06 05:58
在界面中添加一个TextBox控件,或其他支持拖拽属性的控件。
属性:
事件:
private void Form_DragEnter(object sender, DragEventArgs e){ dragEnter(e);}private void Form_DragDrop(object sender, DragEventArgs e){ TextBox textBox = sender as TextBox; textBox.Text = dragDrop(e);}/// <summary>/// 文件拖进事件处理:/// </summary>public void dragEnter(DragEventArgs e){ if (e.Data.GetDataPresent(DataFormats.FileDrop)) //判断拖来的是否是文件 e.Effect = DragDropEffects.Link; //是则将拖动源中的数据连接到控件 else e.Effect = DragDropEffects.None;}/// <summary>/// 文件放下事件处理:/// 放下, 另外需设置对应控件的 AllowDrop = true; /// 获取的文件名形如 "d:\1.txt;d:\2.txt"/// </summary>public string dragDrop(DragEventArgs e){ StringBuilder filesName = new StringBuilder(""); Array file = (System.Array)e.Data.GetData(DataFormats.FileDrop);//将拖来的数据转化为数组存储 foreach (object I in file) { string str = I.ToString(); System.IO.FileInfo info = new System.IO.FileInfo(str); //若为目录,则获取目录下所有子文件名 if ((info.Attributes & System.IO.FileAttributes.Directory) != 0) { str = getAllFiles(str); if (!str.Equals("")) filesName.Append((filesName.Length == 0 ? "" : ";") + str); } //若为文件,则获取文件名 else if (System.IO.File.Exists(str)) filesName.Append((filesName.Length == 0 ? "" : ";") + str); } return filesName.ToString();}/// <summary>/// 判断path是否为目录/// </summary>public bool IsDirectory(String path){ System.IO.FileInfo info = new System.IO.FileInfo(path); return (info.Attributes & System.IO.FileAttributes.Directory) != 0;}/// <summary>/// 获取目录path下所有子文件名/// </summary>public string getAllFiles(String path){ StringBuilder str = new StringBuilder(""); if (System.IO.Directory.Exists(path)) { //所有子文件名 string[] files = System.IO.Directory.GetFiles(path); foreach (string file in files) str.Append((str.Length == 0 ? "" : ";") + file); //所有子目录名 string[] Dirs = System.IO.Directory.GetDirectories(path); foreach (string dir in Dirs) { string tmp = getAllFiles(dir); //子目录下所有子文件名 if (!tmp.Equals("")) str.Append((str.Length == 0 ? "" : ";") + tmp); } } return str.ToString();}
0 0
- C#文件文件夹拖拽功能的实现
- C# winform窗体实现文件拖拽打开的功能
- C#窗体实现文件拖拽功能
- PHP实现删除文件夹内的文件的功能
- 如何实现打开文件所在文件夹的功能(打开所在文件夹并选中文件)
- C#读取文件夹下面的全部文件的实现
- C#实现本地复制文件或者文件夹建立副本取名功能
- ebs文件夹功能的实现
- html5实现文件的拖拽功能
- C#实现文件打印功能
- C# 文件,文件夹的操作
- C#选择文件夹的实现
- C# 实现文件、文件夹进行排序
- 跨平台实现删除文件夹中所有文件、移动文件的功能
- C# SQL文件执行器的功能实现
- C#怎么实现文件下载功能的四种方法
- c# winform TreeView控件中实现拖拽的功能
- c# winform TreeView控件中实现拖拽的功能
- 【新浪微博项目】03微博项目MVC管理与IOS7优化
- cpp反汇编之共同继承一个父类
- 用bat文件备份sqlserver数据库,在通过spring的schedule执行runtime完成周期备份
- 使用镜像站同步android sdk
- win8系统安装mingw遇到的问题
- C#文件文件夹拖拽功能的实现
- Oracle 获取本周、本月、本季、本年的第一天和最后一天
- 字符串复制
- 面试总结-百度(2)
- 解决eclipse中出现Resource is out of sync with the file system问题
- Oracle EBS预警系统管理-基础设置(1)
- Android图片库Fresco
- Android手机GPS卫星定位与通过WLAN与移动网络定位的区别?
- windows消息体制