我尝试将该选项添加到我的应用程序中以将文件拖到我的Listbox
中,而不是导航到文件夹中,这就是我尝试过的:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
listBoxFiles.Items.Add(e.Data.ToString());
}
但不是完整的文件路径e.Data.ToString()
return System.Windows.Forms.DataObject
答案 0 :(得分:6)
此代码我找到了here:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
listBoxFiles.Items.Add(file);
}
答案 1 :(得分:1)
我尝试在WPF中使用@AsfK的答案,不得不删除
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
来自public MainWindow()
其他我删除了拖动的文件。
谢谢!