【问题标题】:How do I support dragging and dropping a folder in Winforms?Winforms中如何支持拖放文件夹?
【发布时间】:2012-06-29 01:41:02
【问题描述】:

我想知道如何拖放文件夹并获取其名称。我已经知道如何处理文件,但我不确定如何修改它以便也能够拖动文件夹。以下是删除文件时触发的事件代码:

private void checkedListBox_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        // NB: I'm only interested in the first file, even if there were
        // multiple files dropped
        string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
    }
}

【问题讨论】:

标签: c# winforms drag-and-drop


【解决方案1】:

您可以测试路径是否为文件夹,并在您的DragEnter handler 中,有条件地更改Effect

 void Target_DragEnter(object sender, DragEventArgs e)
 {
     DragDropEffects effects = DragDropEffects.None;
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
         var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
         if (Directory.Exists(path))
             effects = DragDropEffects.Copy;
     }

     e.Effect = effects;
 }

【讨论】:

  • 我只是写了我自己的答案,但既然你提供了我的链接,我会接受你的。谢谢! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 2011-05-14
  • 1970-01-01
  • 2018-09-03
  • 2020-02-10
相关资源
最近更新 更多