Пример #1
0
        /// <summary>Delete a file to the recycle bin. 'flags' should be Win32.FOF_??? flags</summary>
        public static bool ShellDelete(IntPtr hwnd, string filepath, EFileOpFlags flags = EFileOpFlags.AllowUndo, string title = "Deleting Files...")
        {
            // Convert the filepath to a byte buffer so that we can ensure double null termination.
            // Interop as a string causes the double null not to be copied
            var srcs = Encoding.Unicode.GetBytes(filepath + "\0\0");

            using (var spin = new PinnedObject <byte[]>(srcs, GCHandleType.Pinned))
            {
                if (Environment.Is64BitProcess)
                {
                    var shf = new Shell32.SHFILEOPSTRUCTW64();
                    shf.hwnd              = hwnd;
                    shf.wFunc             = Shell32.FO_DELETE;
                    shf.fFlags            = unchecked ((ushort)flags);
                    shf.pFrom             = spin.Pointer;
                    shf.lpszProgressTitle = title;
                    Shell32.SHFileOperationW(ref shf);
                    return(shf.fAnyOperationsAborted == 0);
                }
                else
                {
                    var shf = new Shell32.SHFILEOPSTRUCTW32();
                    shf.hwnd              = hwnd;
                    shf.wFunc             = Shell32.FO_DELETE;
                    shf.fFlags            = unchecked ((ushort)flags);
                    shf.pFrom             = spin.Pointer;
                    shf.lpszProgressTitle = title;
                    Shell32.SHFileOperationW(ref shf);
                    return(shf.fAnyOperationsAborted == 0);
                }
            }
        }
Пример #2
0
        public static bool ShellMove(IntPtr hwnd, IEnumerable <string> src, IEnumerable <string> dst, EFileOpFlags flags = EFileOpFlags.None, string title = "Moving Files...")
        {
            // Get the list of files as an array
            var src_list = src.ToArray();
            var dst_list = dst.ToArray();

            // Sanity check
            if ((src_list.Length == 0 && dst_list.Length != 0) ||
                (src_list.Length != 0 && dst_list.Length == 0) ||
                (src_list.Length != 0 && dst_list.Length != 1 && dst_list.Length != src_list.Length))
            {
                throw new Exception("Illogical combination of source and destination file paths");
            }

            // Add the multiple files flag
            if (dst_list.Length > 1)
            {
                flags |= EFileOpFlags.MultipleDstFiles;
            }

            // Convert the filepaths to a byte buffer so that we can ensure double null termination.
            // Interop as a string causes the double null not to be copied
            var srcs = Encoding.Unicode.GetBytes(string.Join("\0", src_list) + "\0\0");           // ensure double null termination
            var dsts = Encoding.Unicode.GetBytes(string.Join("\0", dst_list) + "\0\0");           // ensure double null termination

            using (var spin = new PinnedObject <byte[]>(srcs, GCHandleType.Pinned))
                using (var dpin = new PinnedObject <byte[]>(dsts, GCHandleType.Pinned))
                {
                    if (Environment.Is64BitProcess)
                    {
                        var shf = new Shell32.SHFILEOPSTRUCTW64();
                        shf.hwnd              = hwnd;
                        shf.wFunc             = Shell32.FO_MOVE;
                        shf.fFlags            = unchecked ((ushort)flags);
                        shf.pFrom             = spin.Pointer;
                        shf.pTo               = dpin.Pointer;
                        shf.lpszProgressTitle = title;
                        Shell32.SHFileOperationW(ref shf);
                        return(shf.fAnyOperationsAborted == 0);
                    }
                    else
                    {
                        var shf = new Shell32.SHFILEOPSTRUCTW32();
                        shf.hwnd              = hwnd;
                        shf.wFunc             = Shell32.FO_MOVE;
                        shf.fFlags            = unchecked ((ushort)flags);
                        shf.pFrom             = spin.Pointer;
                        shf.pTo               = dpin.Pointer;
                        shf.lpszProgressTitle = title;
                        Shell32.SHFileOperationW(ref shf);
                        return(shf.fAnyOperationsAborted == 0);
                    }
                }
        }