步驟3 為ListVIEw 添加 DragEnter 事件
DragEnter 事件在其他應用程序拖入的文件進入時判斷當前拖動的對象類型,如果是文件類型,則設置拖動響應類型為Copy.
private void listVIEwFolder_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
步驟4 為ListVIEw 添加 DragDrop 事件
DragDrop 事件在這裡完成將其他應用程序拖入的文件拷貝到Winform應用當前的目錄中。
private void listVIEwFolder_DragDrop(object sender, DragEventArgs e)
{
try
{
String[] files = e.Data.GetData(DataFormats.FileDrop, false) as String[];
//Copy file from external application
foreach (string srcfile in files)
{
string destFile = labelCurFolder.Text + "\\" + System.IO.Path.GetFileName(srcfile);
if (System.IO.File.Exists(destFile))
{
if (MessageBox.Show(string.Format(
"This folder already contains a file named {0}, would you like to replace the existing file",
System.IO.Path.GetFileName(srcfile)),
"Confirm File Replace", MessageBoxButtons.YesNo, MessageBoxIcon.None) !=
DialogResult.Yes)
{
continue;
}
}
System.IO.File.Copy(srcfile, destFile, true);
}
//List current folder
ListFolder();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
完成上述4步後,拖入功能就實現了。下面步驟完成拖出功能