简体   繁体   English

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

[英]How Do I use Drag And Drop To Export A File (C#)

I am currently using a file dialog to export a file, but I was wondering how I could export my file using drag and drop. 我目前正在使用文件对话框导出文件,但是我想知道如何使用拖放操作导出文件。 I couldn't figure out how to get the file path of where the item is being dropped. 我不知道如何获取要删除项目的文件路径。 Here is the code that I used for open file dialogue in case its required. 这是我在需要时用于打开文件对话的代码。

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

If you want it to be useful through "drag and drop" you would require some sort of graphical interface that displays the files in a container where they are and then another container to where you want to move them. 如果希望通过“拖放”使它有用,则需要某种图形界面,该界面在文件所在的容器中显示文件,然后在要移动它们的另一个容器中显示文件。 When you highlight an item with your mouse you add them to your itemList and when your drop them you copy them. 当您用鼠标突出显示一个项目时,会将其添加到itemList中,而在放下它们时将它们复制。 Just make sure the List is emptied once in case you remove the highlight. 只需确保清空列表一次,以防删除突出显示。

You don't need the path of where the file is being dropped. 您不需要放置文件的路径。 Instead you need to create a temporary file. 相反,您需要创建一个临时文件。

  1. Save file to a temporary folder 将文件保存到临时文件夹
  2. Initiate drag on an event/command, such as mouse down, in the following way: 通过以下方式在事件/命令(例如,鼠标按下)上拖动:
//(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);

For WinForms example and more details see: Implement file dragging to the desktop from a .net winforms application? 有关WinForms示例和更多详细信息,请参见: 实现将文件从.net winforms应用程序拖动到桌面上吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM