/// <summary>Gets the array of FileNames from the FileGroupDescriptors format.</summary> public static string[] GetFileContentNames(this System.Windows.Forms.IDataObject data) { var names = new string[data.GetFileContentCount()]; if (names.Length != 0) { var bytes = data.GetFileGroupDescriptor().ToArray(); IntPtr fgdPtr = IntPtr.Zero; try { fgdPtr = Marshal.AllocHGlobal(bytes.Length); int offset = Marshal.SizeOf(typeof(UInt32)); int size = Marshal.SizeOf(typeof(FILEDESCRIPTORW)); for (int i = 0; i < names.Length; i++) { var fd = (FILEDESCRIPTORW)Marshal.PtrToStructure(fgdPtr + offset + (i * size), typeof(FILEDESCRIPTORW)); names[i] = fd.cFileName; } } finally { if (fgdPtr != IntPtr.Zero) { Marshal.FreeHGlobal(fgdPtr); } } } return(names); }
/// <summary>Gets the number of files available in the FileGroupDescriptor format.</summary> public static int GetFileContentCount(this System.Windows.Forms.IDataObject data) { // File count is stored as an UInt32 in the FileGroupDescriptor format MemoryStream ms = data.GetFileGroupDescriptor(); if (ms == null) { return(0); } using (var reader = new BinaryReader(ms)) { return((int)reader.ReadUInt32()); // Assume this won't overflow! } }