Beispiel #1
0
        private void Operate(IntPtr handle, List <string> from, string to, FileFuncFlags flag)
        {
            string toDir = to + "\0\0";

            string froms = "";

            foreach (var file in from)
            {
                froms += file + "\0";
            }
            froms += "\0";

            SHFILEOPSTRUCT shfos;

            shfos.hwnd   = handle;
            shfos.wFunc  = flag;
            shfos.pFrom  = froms;
            shfos.pTo    = toDir;
            shfos.fFlags = FILEOP_FLAGS.FOF_ALLOWUNDO;
            shfos.fAnyOperationsAborted = true;
            shfos.hNameMappings         = IntPtr.Zero;
            shfos.lpszProgressTitle     = null;

            SHFileOperation(ref shfos);
        }
Beispiel #2
0
            private static int InternalExec(FileFuncFlags flag, string sourceFileName, string destinationFileName, bool showDialog, bool showProgress, bool autoRename)
            {
                SHFILEOPSTRUCT lpFileOp = new SHFILEOPSTRUCT
                {
                    wFunc  = flag,
                    pFrom  = sourceFileName + "\0",
                    pTo    = destinationFileName + "\0\0",
                    fFlags = FILEOP_FLAGS.FOF_NOERRORUI
                };

                lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMMKDIR;
                if (!showDialog)
                {
                    lpFileOp.fFlags |= FILEOP_FLAGS.FOF_NOCONFIRMATION;
                }
                if (!showProgress)
                {
                    lpFileOp.fFlags |= FILEOP_FLAGS.FOF_SILENT;
                }
                if (autoRename)
                {
                    lpFileOp.fFlags |= FILEOP_FLAGS.FOF_RENAMEONCOLLISION;
                }

                lpFileOp.fAnyOperationsAborted = true;

                return(SHFileOperation(ref lpFileOp));
            }
        private static void FileOperation(FileFuncFlags operation, IntPtr hwndOwner, string progressTitle, IEnumerable<string> sourcePaths, string destinationPath)
        {
            SHFILEOPSTRUCT fos = new SHFILEOPSTRUCT();
            fos.hwnd = hwndOwner;
            fos.wFunc = operation;

            // Build up the "from" string as a null-delimited set of strings terminated by two nulls.
            StringBuilder paths = new StringBuilder();
            foreach(string path in sourcePaths)
            {
                paths.Append(path);
                paths.Append('\0');
            }
            paths.Append('\0'); // Must end with a double-null. Marshalling may result in a triple-null which is OK.
            fos.pFrom = paths.ToString();

            // The "to" string requires a double-null termination
            fos.pTo = string.Concat(destinationPath, "\0");

            fos.fFlags = FILEOP_FLAGS.FOF_FILESONLY|FILEOP_FLAGS.FOF_NOCONFIRMMKDIR|FILEOP_FLAGS.FOF_NOCOPYSECURITYATTRIBS|FILEOP_FLAGS.FOF_NORECURSION|FILEOP_FLAGS.FOF_RENAMEONCOLLISION;
            fos.fAnyOperationsAborted = false;
            fos.hNameMappings = IntPtr.Zero;
            fos.lpszProgressTitle = progressTitle;

            int result = SHFileOperationW(ref fos);
            if (result != 0) throw new ApplicationException(string.Format("SHFIleOperation failed with code 0x{0:x8}", result));
        }