public static MemoryStream GetFileDescriptor(DragFileInfo fileInfo) { MemoryStream stream = new MemoryStream(); stream.Write(BitConverter.GetBytes(1), 0, sizeof(UInt32)); FILEDESCRIPTOR fileDescriptor = new FILEDESCRIPTOR(); fileDescriptor.cFileName = fileInfo.FileName; Int64 fileWriteTimeUtc = fileInfo.WriteTime.ToFileTimeUtc(); fileDescriptor.ftLastWriteTime.dwHighDateTime = (Int32)(fileWriteTimeUtc >> 32); fileDescriptor.ftLastWriteTime.dwLowDateTime = (Int32)(fileWriteTimeUtc & 0xFFFFFFFF); fileDescriptor.nFileSizeHigh = (UInt32)(fileInfo.FileSize >> 32); fileDescriptor.nFileSizeLow = (UInt32)(fileInfo.FileSize & 0xFFFFFFFF); fileDescriptor.dwFlags = FD_WRITESTIME | FD_FILESIZE | FD_PROGRESSUI; Int32 fileDescriptorSize = Marshal.SizeOf(fileDescriptor); IntPtr fileDescriptorPointer = Marshal.AllocHGlobal(fileDescriptorSize); Byte[] fileDescriptorByteArray = new Byte[fileDescriptorSize]; try { Marshal.StructureToPtr(fileDescriptor, fileDescriptorPointer, true); Marshal.Copy(fileDescriptorPointer, fileDescriptorByteArray, 0, fileDescriptorSize); } finally { Marshal.FreeHGlobal(fileDescriptorPointer); } stream.Write(fileDescriptorByteArray, 0, fileDescriptorByteArray.Length); return(stream); }
private void packTreeView_ItemDrag(object sender, ItemDragEventArgs e) { // Proceed with the drag-and-drop, passing the selected items for if (e.Button == MouseButtons.Left && e.Item is System.Windows.Forms.TreeNode && e.Item != null && ((System.Windows.Forms.TreeNode)e.Item).Tag is PackedFile && ((System.Windows.Forms.TreeNode)e.Item).Tag != null) { var file = ((System.Windows.Forms.TreeNode)e.Item).Tag as PackedFile; if (file != null) { var dataObject = new DataObject(); var filesInfo = new DragFileInfo(file.FullPath, file.Size); using (MemoryStream infoStream = DragDropHelper.GetFileDescriptor(filesInfo), contentStream = DragDropHelper.GetFileContents(file.Data)) { dataObject.SetData(DragDropHelper.CFSTR_FILEDESCRIPTORW, infoStream); dataObject.SetData(DragDropHelper.CFSTR_FILECONTENTS, contentStream); dataObject.SetData(DragDropHelper.CFSTR_PERFORMEDDROPEFFECT, null); DoDragDrop(dataObject, DragDropEffects.All); } } } }