从.net winforms应用程序实现文件拖动到桌面?

mlo*_*mlo 12 .net c# drag-and-drop

我有一个文件列表,其列表框中包含其名称,其内容存储在SQL表中,并希望我的应用程序的用户能够在列表框中选择一个或多个文件名并将其拖到桌面上,从而产生桌面上的实际文件.我找不到有关如何执行此操作的任何文档.谁能解释或指出解释?

稍后添加:我已经能够通过处理DragLeave事件来完成这项工作.在其中,我在临时目录中创建一个文件,其中包含所选名称和从SQL Server中提取的内容.然后我将文件的路径放入对象:

var files = new string[1];
files[0] = "full path to temporary file";
var dob = new DataObject();    
dob.SetData(DataFormats.FileDrop, files);
DoDragDrop(dob, DragDropEffects.Copy);
Run Code Online (Sandbox Code Playgroud)

但这似乎非常低效和笨拙,我还没有想出一个摆脱累积的临时文件的好方法.

BFr*_*ree 11

我可以帮你一点.这里有一些代码可以让你从列表框中拖出一些东西,当它放在桌面上时,它会创建一个存储在你机器上的文件到桌面的副本.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.listBox1.Items.Add("foo.txt");
        this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
        this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);
    }

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

    void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        string[] filesToDrag = 
        {
            "c:/foo.txt"
        };
        this.listBox1.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
    }
}
Run Code Online (Sandbox Code Playgroud)