拖放操作和文件复制小功能
下面的代码没有打入注释,加入了一个ListBox,当文件拖放上来后,讲内容显示在里面
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void lstFilePath_DragDrop(object sender, DragEventArgs e)
{
foreach (string strPath in (string[])e.Data.GetData(DataFormats.FileDrop))
{
lstFilePath.Items.Add(strPath);
}
}
将整个窗体代码都复制下来,是一个复制的小程序,将拖放到LISTBOX里的文件复制到文本框里指定的位置,里面用到了一个外部控件,可以使用普通的button替换之
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace PersonalDisk
{
public partial class frmDrag : Form
{
/// <summary>
/// 获得/设置一个值,判断是否已经存在了一个类的实例
/// </summary>
public static bool IsExist=false;
public frmDrag()
{
InitializeComponent();
frmDrag.IsExist = true;
}
private void frmDrag_MouseDown(object sender, MouseEventArgs e)
{
//如果鼠标指针在标题栏范围内并且按下了鼠标左键,则触发移动标题栏方法
if (e.Button == MouseButtons.Left && e.Y <= 25)
{
Program.ReleaseCapture();
Program.SendMessage(this.Handle, Program.WM_SYSCOMMAND, Program.SC_MOVE + Program.HTCAPTION, 0);
}
}
private void picControlClose_Click(object sender, EventArgs e)
{
frmDrag.IsExist = false;
this.Close();
}
private void picControlMin_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void lstFilePath_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void lstFilePath_DragDrop(object sender, DragEventArgs e)
{
foreach (string strPath in (string[])e.Data.GetData(DataFormats.FileDrop))
{
lstFilePath.Items.Add(strPath);
}
}
private void ExBtnClose_ClickEvent(object sender, EventArgs e)
{
picControlClose_Click(null,null);
}
private void ExBtnClear_ClickEvent(object sender, EventArgs e)
{
lstFilePath.Items.Clear();
}
private void ExBtnDel_ClickEvent(object sender, EventArgs e)
{
lstFilePath.Items.RemoveAt(lstFilePath.SelectedIndex);
}
private void picControlBring_Click(object sender, EventArgs e)
{
if (this.TopMost == true)
{
this.TopMost = false;
picControlBring.Image = PersonalDisk.Properties.Resources.btnBottom;
}
else
{
this.TopMost = true;
picControlBring.Image = PersonalDisk.Properties.Resources.btnTop;
}
}
private void ExBtnExecute_ClickEvent(object sender, EventArgs e)
{
ExBtnExecute.CtlEnabled = false;
for (int i = 0; i < lstFilePath.Items.Count; i++)
{
Application.DoEvents();
lstFilePath.SetSelected(i, true);
//如果当前字符串是一个目录则.
if (Directory.Exists(lstFilePath.Items[i].ToString()))
{
CopyDirectory(lstFilePath.Items[i].ToString(), txtSelDrive.Text +"\\"+ Path.GetFileName(lstFilePath.Items[i].ToString()));
lstFilePath.Items[i] = "复制完成";
}
else
{
if (!File.Exists(lstFilePath.Items[i].ToString())) continue; //如果文件不存在继续下一个循环
File.Copy(lstFilePath.Items[i].ToString(), txtSelDrive.Text + "\\" + Path.GetFileName(lstFilePath.Items[i].ToString()));
lstFilePath.Items[i] = "复制完成";
}
}
lstFilePath.Items.Clear();
ExBtnExecute.CtlEnabled = true;
}
private void txtSelDrive_DoubleClick(object sender, EventArgs e)
{
FolderSelDialog.ShowDialog(this);
txtSelDrive.Text = FolderSelDialog.SelectedPath;
}
/// <summary>
/// 复制一个目录下的所有文件或目录到一个新的目录下
/// </summary>
/// <param name="sourcePath">源目录路径</param>
/// <param name="destPath">目标目录路径</param>
private void CopyDirectory(string sourcePath, string destPath)
{
try
{
//如果目标路径没有以\结尾则加之
if (destPath[destPath.Length - 1] != Path.DirectorySeparatorChar)
{
destPath += Path.DirectorySeparatorChar;
}
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
string[] fileList = Directory.GetFileSystemEntries(sourcePath);
foreach (string file in fileList)
{
//如果是一个目录则
if (Directory.Exists(file))
{
CopyDirectory(file, destPath + Path.GetFileName(file));
}
else
{
File.Copy(file, destPath + Path.GetFileName(file),true);
}
}
}
catch(IOException ioe)
{
MessageBox.Show(ioe.Message, "复制文件时出错", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
拖放操作和文件复制小功能相关推荐
- Linux命令操作,文件复制,删除修改等
cp命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或目录 目标文件或目录 说明:该命令把指定的源文件复制 ...
- C语言模拟实现(四)-----利用open、read、write等系统调用函数实现cp(文件复制)功能
话不多说,直接上代码! #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #incl ...
- java操作大文件复制
https://www.cnblogs.com/coprince/p/6594348.html https://blog.csdn.net/w592376568/article/details/796 ...
- 如何在 Windows 7 中使用多线程加快文件复制[转]
为什么没办法在 Windows 7 下加快 Windows 7 中文件复制/传输功能呢?可以用 robocopy 实现多线程文件复制实现. 经常进行文件管理操作的朋友们,提到复制/粘贴操作,想必很多人 ...
- 带进度条的文件复制。
今天项目要新增一个文件复制的功能,File.Copy() 没有好的用户体验度,所以想到使用进度条. 1 const Int32 BUFFER_SIZE = 4096; //每次拷贝4K的文件, 只考虑 ...
- TX Text Control文字处理教程(13)实现拖放操作
Samples\WinForms\VB.NET\DragDrop Samples\WinForms\CSharp\DragDrop (一)两种拖放类型 在大多数文字处理器中支持两 ...
- windows强制复制不计算机,Win10复制粘贴功能用不了的修复方法
win10正式版系统最常用的操作就是复制粘贴,有时候遇到复制粘贴功能用不了的情况,降低工作效率.即使重新启动电脑也没办法修复,到底怎么回事?其实我们可以通过注册表来修复的,感兴趣的小伙伴一起阅读教程来 ...
- 10个关于文件操作的小功能(Python),都很实用~
1 优雅的获取文件后缀名 import os file_ext = os.path.splitext('./data/py/test.py') front,ext = file_ext In [5]: ...
- iOS开发学无止境 - NSFileManager文件操作的十个小功能
(配图的小故事还记得嘛) NSFileManager是一个单列类,也是一个文件管理器.可以通过NSFileManager创建文件夹.创建文件.写文件.读文件内容等等基本功能. 下面将介绍NSFileM ...
最新文章
- Cocoapods安装使用
- Java虚拟机学习(4):JDK可视化监控工具
- 【机器学习】Github8.9K,目前最佳模型解释器-LIME!
- c语言继续程序指令,C语言预处理程序
- 怎样判断ios app 第一次启动
- 201521123121 《Java程序设计》第14周学习总结
- 滴滴滴,ITester软件测试小栈所有资源放送!
- linux空磁盘划分,Linux 磁盘划分
- java定义一个getsize方法_java.util.zip.ZipEntry.getSize()方法示例
- echar2 ,3 自定义右键菜单 右击事件
- 短期学习目标2022/3/16
- CSDN上代码块背景颜色的设置
- 频谱、频谱密度、能量谱密度、功率谱密度
- 计算机的基础操作知识试题,[计算机基本操作知识]计算机操作系统基础知识试题...
- mysql修改数据库用户名和密码
- eeepc linux 软件管理,华硕EeePC 901下EEEbuntu 3.0完美优化教程
- 《Python小白入门》python环境安装,一步一截图
- Google收购Moto:天使还是魔鬼?
- 时间同步软件 linux,时间同步(Arch Linux)
- 计算机组成原理-组成篇(上)
热门文章
- 小米AIoT开发者大会28日召开,雷军的AIoT和小米的这一年
- XP/Vista 彻底休矣!Firefox 浏览器放弃支持
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Ruby中爬虫的实现
- 到隔壁便利商店买东西,不要开车!
- 一场稳定、高清、流畅的大型活动直播是怎么炼成的?
- 【转载】究竟啥才是互联网架构“高可用”
- linux上的用户管理
- 图像验证码识别(七)——字符分割
- 传智播客C/C++各种开发环境搭建视频工具文档免费教程