一直都沒試過拖放操作的用處,今天試了試,發現真的很好用!!
下面的代碼沒有打入注釋,加入了一個ListBox,當檔案拖放上來後,講内容顯示在裡面
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);
将整個窗體代碼都複制下來,是一個複制的小程式,将拖放到LISTBOX裡的檔案複制到文本框裡指定的位置,裡面用到了一個外部控件,可以使用普通的button替換之
using System;
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)
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;
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()));
ExBtnExecute.CtlEnabled = true;
private void txtSelDrive_DoubleClick(object sender, EventArgs e)
FolderSelDialog.ShowDialog(this);
txtSelDrive.Text = FolderSelDialog.SelectedPath;
/// 複制一個目錄下的所有檔案或目錄到一個新的目錄下
/// <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);
}
}