3

我想将列表框的元素拖放到资源管理器中。我复制了一个不存在的文件,如本文所述,并对程序代码进行了一些更改:如何使用 filegroupdescriptor 将文件拖动到资源管理器 c#

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.None)
        return;

    byte[] array = Encoding.ASCII.GetBytes("ABCD");
    DataObject dataObject = new DataObject();
    DragFileInfo filesInfo = new DragFileInfo(@"myFile.txt", array);
    MemoryStream infoStream = GetFileDescriptor(filesInfo);
    MemoryStream contentStream = GetFileContents(filesInfo);
    dataObject.SetData(CFSTR_FILEDESCRIPTORW, infoStream);
    dataObject.SetData(CFSTR_FILECONTENTS, contentStream);
    dataObject.SetData(CFSTR_PERFORMEDDROPEFFECT, null);
    // drag and drop file with name "myFile.txt" and body "ABCD".
    DoDragDrop(dataObject, DragDropEffects.All);
}
private MemoryStream GetFileContents(DragFileInfo fileInfo)
{
    MemoryStream stream = new MemoryStream();
    if (fileInfo.SourceFileBody.Length == 0) fileInfo.SourceFileBody = new Byte[1];
    stream.Write(fileInfo.SourceFileBody, 0, fileInfo.SourceFileBody.Length);
    return stream;
}

public struct DragFileInfo
{ 
    public string FileName; 
    public byte[] SourceFileBody; 
    public DateTime WriteTime; 
    public Int64 FileSize;

    public DragFileInfo(string fileName, byte[] sourceFileBody)
    {
        FileName = fileName;
        SourceFileBody = sourceFileBody;
        WriteTime = DateTime.Now;
        FileSize = sourceFileBody.Length;
    }
}

这工作正常,但我需要同时拖放多个文件。我怎样才能做到这一点?

4

1 回答 1

1

我有同样的问题。我在那里找到了解决方案:http: //www.codeproject.com/Articles/23139/Transferring-Virtual-Files-to-Windows-Explorer-in

您需要重写 DataObject 类的方法:GetData(); 例如:

 public override object GetData(string format, bool autoConvert)
    {
        if (String.Compare(format, CFSTR_FILECONTENTS, StringComparison.OrdinalIgnoreCase) == 0)
        {
            base.SetData(CFSTR_FILECONTENTS, GetFileContents(FileIndex++));
        }
        return base.GetData(format, autoConvert);
    }
于 2013-01-25T07:49:54.983 回答