在論壇上看見這篇文章
如何提供視覺 C# 應用程式中的檔案拖放功能,才想到之前練習時,並沒有真的了解 DragEnter 內的 Code,代表甚麼意義,趁這個機會了解一下,並延伸這篇文章,拖曳物件假如是資料夾,必須把資料夾內的檔案也列出
using System.IO;
namespace DragDropList
{
public partial class DragDropList : Form
{
public DragDropList()
{
InitializeComponent();
}
private void DragDropList_Load(object sender, EventArgs e)
{
listBox.AllowDrop = true;
listBox.Dock = DockStyle.Fill;
}
private void listBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox_DragDrop(object sender, DragEventArgs e)
{
listBox.Items.Clear();
string[] entriesPath = (string[])e.Data.GetData(DataFormats.FileDrop,false) ;
foreach (string entryPath in entriesPath)
{
listBox.Items.Add(entryPath);
dirFiles(entryPath);
}
}
private void dirFiles(string _entryPath)
{
DirectoryInfo dirInfo = new DirectoryInfo(_entryPath);
if (dirInfo.Exists == false) return;
foreach (FileSystemInfo info in dirInfo.GetFileSystemInfos())
{
listBox.Items.Add(info.FullName);
dirFiles(info.FullName);
}
}
}
}
沒有留言:
張貼留言