【问题标题】:How Do I use Drag And Drop To Export A File (C#)如何使用拖放导出文件 (C#)
【发布时间】:2017-07-18 21:08:52
【问题描述】:

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

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);
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    1. 将文件保存到临时文件夹
    2. 通过以下方式启动事件/命令的拖动,例如鼠标按下:
    //(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?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-07-20
        • 2016-09-21
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多