0

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

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];
    }
}
4

1 回答 1

7

您可以测试路径是否为文件夹,并在您的DragEnter处理程序中,有条件地更改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;
 }
于 2012-06-27T15:23:10.277 回答