实施将文件从.net winforms应用程序拖到桌面上的方法? [英] Implement file dragging to the desktop from a .net winforms application?

查看:95
本文介绍了实施将文件从.net winforms应用程序拖到桌面上的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of files with their names in a listbox and their contents stored in an SQL table and want the user of my app to be able to select one or more of the filenames in the listbox and drag them to the desktop, yielding the actual files on the desktop. I can't find any documentation on how to do this. Can anyone explain or point to an explanation?

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

Added later: I've been able to make this work by handling the DragLeave event. In it I create a file in a temporary directory with the selected name and the contents pulled from SQL Server. I then put the path to the file into the object:

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

但这似乎效率很低而且笨拙,我还没有找到摆脱的好方法

But this seems very inefficient and clumsy, and I have not yet figured out a good way to get rid of accumulated temp files.

推荐答案

我可以为您提供一些帮助。这是一些代码,可让您将某些内容拖出列表框,并将其拖放到桌面上时,它将创建计算机上存在的文件的副本到桌面。

I can help you somewhat. Here's some code that will allow you to drag something out of the listbox, and when dropped on the desktop, it will create a copy of a file that exists on your machine to the desktop.

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

这篇关于实施将文件从.net winforms应用程序拖到桌面上的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆