如何使用拖放导出文件(C#)

时间:2017-07-18 21:08:52

标签: c# .net drag-and-drop

我目前正在使用文件对话框导出文件,但我想知道如何使用拖放功能导出文件。我无法弄清楚如何获取项目被删除的文件路径。这是我用于打开文件对话的代码,如果需要的话。

if (this.listView1.SelectedItems.Count > 0)
{
    ListViewItem item = this.listView1.SelectedItems[0];
    string text = this.faderLabel8.Text;
    if (!text.EndsWith(@"\"))
    {
        text = text + @"\";
    }

    using (SaveFileDialog dialog = new SaveFileDialog())
    {
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            Jtag.ReceiveFile(item.SubItems[0].Text, text + item.SubItems[0].Text);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果你希望它通过"拖放"你需要某种图形界面,在它们所在的容器中显示文件,然后在另一个容器中显示你要移动它们的位置。当您使用鼠标突出显示某个项目时,您可以将它们添加到itemList中,然后在删除它们时复制它们。如果删除突出显示,只需确保列表清空一次。

答案 1 :(得分:0)

您不需要删除文件的路径。相反,您需要创建一个临时文件。

  1. 将文件保存到临时文件夹
  2. 按以下方式启动对事件/命令的拖动,例如鼠标按下:
  3. //(This example is uses WPF/System.Windows.DragDrop)
    //Create temporary file
    string fileName = "DragDropSample.txt";
    var tempPath = System.IO.Path.GetTempPath();
    var tempFilePath = System.IO.Path.Combine(tempPath, fileName);
    System.IO.File.WriteAllText(tempFilePath, "Testing drag and drop");
    //Create DataObject to drag
    DataObject dragData = new DataObject();
    dragData.SetData(DataFormats.FileDrop, new string[] { tempFilePath });
    //Initiate drag/drop
    DragDrop.DoDragDrop(dragSourceElement, dragData, DragDropEffects.Move);
    

    对于WinForms示例和更多详细信息,请参阅: Implement file dragging to the desktop from a .net winforms application?