-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDragFile.cs
77 lines (66 loc) · 2.65 KB
/
DragFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace NbuExplorer
{
public class DragFile
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct FILEDESCRIPTOR
{
public UInt32 dwFlags;
public Guid clsid;
public System.Drawing.Size sizel;
public System.Drawing.Point pointl;
public UInt32 dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public UInt32 nFileSizeHigh;
public UInt32 nFileSizeLow;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public String cFileName;
}
public const string CFSTR_PREFERREDDROPEFFECT = "Preferred DropEffect";
public const string CFSTR_PERFORMEDDROPEFFECT = "Performed DropEffect";
public const string CFSTR_FILEDESCRIPTORW = "FileGroupDescriptorW";
public const string CFSTR_FILECONTENTS = "FileContents";
public const Int32 FD_WRITESTIME = 0x00000020;
public const Int32 FD_FILESIZE = 0x00000040;
public const Int32 FD_PROGRESSUI = 0x00004000;
public struct DragFileInfo
{
public string FileName;
public string SourceFileName;
public DateTime WriteTime;
public Int64 FileSize;
}
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;
}
}
}