Exemplo n.º 1
0
        /// <summary>
        /// Invokes SHFileOperation in managed code. Refer to https://dev.winodws.com or the Windows SDK for more information.
        /// </summary>
        /// <example>
        /// ShellLib.ShellFileOperation fo = new ShellLib.ShellFileOperation();
        /// string[] source = new string[nCount];
        /// string[] dest = new string[nCount];
        /// 
        /// .. add full paths to source and dest ...
        /// 
        /// fo.Operation = ShellLib.ShellApi.FileOperations.FO_COPY;
        /// fo.OwnerWindow = this.Handle;
        /// fo.ProgressTitle = "Copying Checked Files";
        /// fo.SourceFiles = source;
        /// fo.DestFiles = dest;
        /// 
        /// bool RetVal = fo.DoOperation();
        /// </example>
        /// <returns>Indicates if the operation was successful.</returns>
        public bool DoOperation()
        {
            ShellApi.SHFILEOPSTRUCT FileOpStruct = new ShellApi.SHFILEOPSTRUCT();

            FileOpStruct.hwnd = OwnerWindow;
            FileOpStruct.wFunc = (uint)Operation;

            String multiSource = StringArrayToMultiString(SourceFiles);
            String multiDest = StringArrayToMultiString(DestFiles);
            FileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource);
            FileOpStruct.pTo = Marshal.StringToHGlobalUni(multiDest);

            FileOpStruct.fFlags = (ushort)OperationFlags;
            FileOpStruct.lpszProgressTitle = ProgressTitle;
            FileOpStruct.fAnyOperationsAborted = 0;
            FileOpStruct.hNameMappings = IntPtr.Zero;

            int RetVal;
            RetVal = ShellApi.SHFileOperation(ref FileOpStruct);

            ShellApi.SHChangeNotify(
                (uint)ShellApi.ShellChangeNotificationEvents.SHCNE_ALLEVENTS,
                (uint)ShellApi.ShellChangeNotificationFlags.SHCNF_DWORD,
                IntPtr.Zero,
                IntPtr.Zero);

            if (RetVal != 0)
                return false;

            if (FileOpStruct.fAnyOperationsAborted != 0)
                return false;

            return true;
        }
Exemplo n.º 2
0
        private bool DeleteFile(FileItem file)
        {
            var sfo = new ShellApi.SHFILEOPSTRUCT()
            {
                wFunc  = ShellApi.FileFuncFlags.FO_DELETE,
                fFlags = ShellApi.FILEOP_FLAGS.FOF_ALLOWUNDO | ShellApi.FILEOP_FLAGS.FOF_NOCONFIRMATION,
                pFrom  = file.GetFullPath() + char.MinValue + char.MinValue
            };

            return(ShellApi.SHFileOperation(ref sfo) == 0);
        }
Exemplo n.º 3
0
        private bool DeleteFiles(string[] filePaths)
        {
            var totalSize = filePaths.Sum(x => x.Length + 1) + 1;
            var sb        = new System.Text.StringBuilder(totalSize);

            foreach (var filePath in filePaths)
            {
                sb.Append(filePath + '\0');
            }
            sb.Append('\0');
            var sfo = new ShellApi.SHFILEOPSTRUCT()
            {
                wFunc  = ShellApi.FileFuncFlags.FO_DELETE,
                fFlags = ShellApi.FILEOP_FLAGS.FOF_ALLOWUNDO | ShellApi.FILEOP_FLAGS.FOF_NOCONFIRMATION,
                pFrom  = sb.ToString()
            };

            return(ShellApi.SHFileOperation(ref sfo) == 0);
        }
Exemplo n.º 4
0
        public bool DoOperation()
        {
            ShellApi.SHFILEOPSTRUCT FileOpStruct = new ShellApi.SHFILEOPSTRUCT();

            FileOpStruct.hwnd  = OwnerWindow;
            FileOpStruct.wFunc = (uint)Operation;

            String multiSource = StringArrayToMultiString(SourceFiles);
            String multiDest   = StringArrayToMultiString(DestFiles);

            FileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource);
            FileOpStruct.pTo   = Marshal.StringToHGlobalUni(multiDest);

            FileOpStruct.fFlags                = (ushort)OperationFlags;
            FileOpStruct.lpszProgressTitle     = ProgressTitle;
            FileOpStruct.fAnyOperationsAborted = 0;
            FileOpStruct.hNameMappings         = IntPtr.Zero;
            this.NameMappings = new ShellNameMapping[0];

            int RetVal;

            RetVal = ShellApi.SHFileOperation(ref FileOpStruct);

            ShellApi.SHChangeNotify(
                (uint)ShellChangeNotificationEvents.SHCNE_ALLEVENTS,
                (uint)ShellChangeNotificationFlags.SHCNF_DWORD,
                IntPtr.Zero,
                IntPtr.Zero);

            if (RetVal != 0)
            {
                return(false);
            }

            if (FileOpStruct.fAnyOperationsAborted != 0)
            {
                return(false);
            }

            // Newly added on 2007/08/29 to make hNameMappings work
            if (FileOpStruct.hNameMappings != IntPtr.Zero)
            {
                // Get MappingTable
                ShellApi.SHNAMEMAPPINGINDEXSTRUCT mappingIndex = (ShellApi.SHNAMEMAPPINGINDEXSTRUCT)Marshal.PtrToStructure(
                    FileOpStruct.hNameMappings,
                    typeof(ShellApi.SHNAMEMAPPINGINDEXSTRUCT));

                // Prepare array
                this.NameMappings = new ShellNameMapping[mappingIndex.counter];

                // Set pointer to first mapping struct
                IntPtr mover = mappingIndex.firstMappingStruct;
                for (int i = 0; i < mappingIndex.counter; i++)
                {
                    ShellApi.SHNAMEMAPPINGSTRUCT oneNameMappingStruct =
                        (ShellApi.SHNAMEMAPPINGSTRUCT)Marshal.PtrToStructure(mover, typeof(ShellApi.SHNAMEMAPPINGSTRUCT));

                    this.NameMappings[i] = new ShellNameMapping(oneNameMappingStruct.pszOldPath, oneNameMappingStruct.pszNewPath);

                    // move pointer to the next mapping struct
                    mover = (IntPtr)((int)mover + Marshal.SizeOf(typeof(ShellApi.SHNAMEMAPPINGSTRUCT)));
                }

                // Free NameMappings in memory
                ShellApi.SHFreeNameMappings(FileOpStruct.hNameMappings);
            }

            return(true);
        }
Exemplo n.º 5
0
        public bool DoOperation()
        {
            ShellApi.SHFILEOPSTRUCT FileOpStruct = new ShellApi.SHFILEOPSTRUCT();

            FileOpStruct.hwnd = OwnerWindow;
            FileOpStruct.wFunc = (uint)Operation;

            String multiSource = StringArrayToMultiString(SourceFiles);
            String multiDest = StringArrayToMultiString(DestFiles);
            FileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource);
            FileOpStruct.pTo = Marshal.StringToHGlobalUni(multiDest);

            FileOpStruct.fFlags = (ushort)OperationFlags;
            FileOpStruct.lpszProgressTitle = ProgressTitle;
            FileOpStruct.fAnyOperationsAborted = 0;
            FileOpStruct.hNameMappings = IntPtr.Zero;
            this.NameMappings = new ShellNameMapping[0];

            int RetVal;
            RetVal = ShellApi.SHFileOperation(ref FileOpStruct);

            ShellApi.SHChangeNotify(
                (uint)ShellChangeNotificationEvents.SHCNE_ALLEVENTS,
                (uint)ShellChangeNotificationFlags.SHCNF_DWORD,
                IntPtr.Zero,
                IntPtr.Zero);

            if (RetVal != 0)
                return false;

            if (FileOpStruct.fAnyOperationsAborted != 0)
                return false;

            // Newly added on 2007/08/29 to make hNameMappings work
            if (FileOpStruct.hNameMappings != IntPtr.Zero)
            {
                // Get MappingTable
                ShellApi.SHNAMEMAPPINGINDEXSTRUCT mappingIndex = (ShellApi.SHNAMEMAPPINGINDEXSTRUCT)Marshal.PtrToStructure(
                    FileOpStruct.hNameMappings,
                    typeof(ShellApi.SHNAMEMAPPINGINDEXSTRUCT));

                // Prepare array
                this.NameMappings = new ShellNameMapping[mappingIndex.counter];

                // Set pointer to first mapping struct
                IntPtr mover = mappingIndex.firstMappingStruct;
                for (int i = 0; i < mappingIndex.counter; i++)
                {
                    ShellApi.SHNAMEMAPPINGSTRUCT oneNameMappingStruct =
                        (ShellApi.SHNAMEMAPPINGSTRUCT)Marshal.PtrToStructure(mover, typeof(ShellApi.SHNAMEMAPPINGSTRUCT));

                    this.NameMappings[i] = new ShellNameMapping(oneNameMappingStruct.pszOldPath, oneNameMappingStruct.pszNewPath);

                    // move pointer to the next mapping struct
                    mover = (IntPtr)((int)mover + Marshal.SizeOf(typeof(ShellApi.SHNAMEMAPPINGSTRUCT)));
                }

                // Free NameMappings in memory
                ShellApi.SHFreeNameMappings(FileOpStruct.hNameMappings);
            }

            return true;
        }
Exemplo n.º 6
0
        public CopyFileResult DoOperation(ref String errorMessage)
        {
            errorMessage = "";
            ShellApi.SHFILEOPSTRUCT FileOpStruct = new ShellApi.SHFILEOPSTRUCT();

            FileOpStruct.hwnd = OwnerWindow;
            FileOpStruct.wFunc = (uint)Operation;

            String multiSource = StringArrayToMultiString(SourceFiles);
            String multiDest = StringArrayToMultiString(DestFiles);
            FileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource);
            FileOpStruct.pTo = Marshal.StringToHGlobalUni(multiDest);

            FileOpStruct.fFlags = (ushort)OperationFlags;
            FileOpStruct.lpszProgressTitle = ProgressTitle;
            FileOpStruct.fAnyOperationsAborted = 0;
            FileOpStruct.hNameMappings = IntPtr.Zero;

            RetVal = ShellApi.SHFileOperation(ref FileOpStruct);

            ShellApi.SHChangeNotify(
                (uint)ShellChangeNotificationEvents.SHCNE_ALLEVENTS,
                (uint)ShellChangeNotificationFlags.SHCNF_DWORD,
                IntPtr.Zero,
                IntPtr.Zero);

            if (FileOpStruct.fAnyOperationsAborted != 0)
                return CopyFileResult.Aborted;

            // If RetVal is 0, we are then sure that the operation succeeded.
            if (RetVal == 0)
                return CopyFileResult.Completed;

            // In this case, the operation failed. We need to report why.
            // From http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx,
            // we cannot map some of the return values of ShFileOperation() to Win32 error
            // strings, let's use a custom dictionnary to perform the appropriate mapping.
            if (ShFileOpErrorMap.ContainsKey(RetVal)) errorMessage = ShFileOpErrorMap[RetVal];
            else errorMessage = "Unknown error message: " + RetVal;
            return CopyFileResult.Failed;
        }