예제 #1
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);
        }
예제 #2
0
        public bool DoOperation()
        {
            if (Silent)
            {
                OperationFlags |= ShellFileOperationFlags.FOF_NOCONFIRMATION;
            }

            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)ShellChangeNotificationEvents.SHCNE_ALLEVENTS,
                (uint)ShellChangeNotificationFlags.SHCNF_DWORD,
                IntPtr.Zero,
                IntPtr.Zero);

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

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

            return(true);
        }
예제 #3
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);



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

            if (FileOpStruct.fAnyOperationsAborted != 0)
            {
                return(false);
            }
            return(true);
        }
예제 #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)
            {
                throw new Exception("File Operation Failed : Errno " + RetVal);
            }

            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);
        }