将一些文件/文件夹拖放到ListView中

时间:2017-05-22 18:18:18

标签: c# winforms

enter image description here

我有一个应用程序文件管理器,它包含listview1和listview2。我想将一个文件或文件夹从listview1拖到listview2,相反,但它无法正常工作。 我有以下代码:

       private void listView_ItemDrag(object sender, ItemDragEventArgs e)
       {
          listView.DoDragDrop(listView.SelectedItems[0].Tag.ToString(), DragDropEffects.Copy);
       }

    private void listView_DragDrop(object sender, DragEventArgs e)
    {
        ListView lv = (ListView)sender;

        // Returns the address of the mouse pointer on the ListView Control.
        Point cp = lv.PointToClient(new Point(e.X, e.Y));

        // Get the item at the specified position of the cursor.
        ListViewItem dragToItem = lv.GetItemAt(cp.X, cp.Y);

        if (dragToItem == null)
        {
            return;
        }

        string dest = dragToItem.Tag.ToString();
        DirectoryInfo dri = new DirectoryInfo(dest);

        // Declare the string variable containing the path containing the destination file
        string dest1 = Path.GetDirectoryName(dest);

        string s = (string)(e.Data.GetData(DataFormats.Text));

        DirectoryInfo dir = new DirectoryInfo(s);
        string fileName = "\\" + Path.GetFileName(s);


        // Drag and drop are a file
        if (dri.Attributes != FileAttributes.Directory)
        {
            dest1 = dest1 + "\\" + Path.GetFileName(s);

            if (dir.Attributes == FileAttributes.Directory)
            {
                FileSystem.CopyDirectory(s, dest1, UIOption.AllDialogs);
            }
            else
            {
                FileSystem.CopyFile(s, dest1, UIOption.AllDialogs);
            }

            LoadListViewSubFiles(Path.GetDirectoryName(dest1));
        }
        else
        {
            dest = dest + fileName;

            if (dir.Attributes == FileAttributes.Directory)
            {
                FileSystem.CopyDirectory(s, dest, UIOption.AllDialogs);
                LoadListViewSubFiles(dri.ToString());
            }
            else
            {
                FileSystem.CopyFile(s, dest, UIOption.AllDialogs);
            }

        }
    }

    private void listView_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

0 个答案:

没有答案