Example #1
0
        public static MemoryStream GenerateFileDescriptor(List <FileAttributes> files)
        {
            try
            {
                MemoryStream FileDescriptorMemoryStream = new MemoryStream();
                // Write out the FILEGROUPDESCRIPTOR.cItems value
                FileDescriptorMemoryStream.Write(BitConverter.GetBytes(files.Count), 0, sizeof(UInt32));

                FILEDESCRIPTOR FileDescriptor = new FILEDESCRIPTOR();
                foreach (var si in files)
                {
                    string n = si.RelativePath;

                    //If the file is in the root folder of the operation, the relative path will not be set!
                    if (string.IsNullOrEmpty(si.RelativePath))
                    {
                        n = si.FileName;
                    }

                    FileDescriptor.cFileName = n;
                    Int64 FileWriteTimeUtc = si.LastChangeTime.ToFileTimeUtc();
                    FileDescriptor.ftLastWriteTime.dwHighDateTime = (Int32)(FileWriteTimeUtc >> 32);
                    FileDescriptor.ftLastWriteTime.dwLowDateTime  = (Int32)(FileWriteTimeUtc & 0xFFFFFFFF);
                    FileDescriptor.nFileSizeHigh = (UInt32)(si.FileSize >> 32);
                    FileDescriptor.nFileSizeLow  = (UInt32)(si.FileSize & 0xFFFFFFFF);
                    FileDescriptor.dwFlags       = FD_WRITESTIME | FD_FILESIZE | FD_PROGRESSUI;

                    Int32  FileDescriptorSize    = Marshal.SizeOf(FileDescriptor);
                    IntPtr FileDescriptorPointer = Marshal.AllocHGlobal(FileDescriptorSize);
                    Marshal.StructureToPtr(FileDescriptor, FileDescriptorPointer, true);
                    Byte[] FileDescriptorByteArray = new Byte[FileDescriptorSize];
                    Marshal.Copy(FileDescriptorPointer, FileDescriptorByteArray, 0, FileDescriptorSize);
                    Marshal.FreeHGlobal(FileDescriptorPointer);
                    FileDescriptorMemoryStream.Write(FileDescriptorByteArray, 0, FileDescriptorByteArray.Length);
                }
                return(FileDescriptorMemoryStream);
            }
            catch (Exception ex)
            {
                ISLogger.Write("Get file descriptor failed: " + ex.Message);
                return(null);
            }
        }
        private void GetDataFileDescriptor(ref FORMATETC format, ref STGMEDIUM medium)
        {
            try
            {
                if (format.tymed != TYMED.TYMED_HGLOBAL)
                {
                    ISLogger.Write("Shell requested file descriptor via tymed {0}. Not supported", format.tymed);
                    return;
                }

                if (fileDescriptorStream == null)
                {
                    fileDescriptorStream = FILEDESCRIPTOR.GenerateFileDescriptor(storedFiles.AllFiles);
                }

                byte[] desc = fileDescriptorStream.ToArray();
                medium.tymed       = TYMED.TYMED_HGLOBAL;
                medium.unionmember = CopyToHGlobal(desc);
            }catch (Exception ex)
            {
                ISLogger.Write("Failed to transfer file descriptor to shell: " + ex.Message);
            }
        }