Esempio n. 1
0
        /// <summary>
        /// Send file to recycle bin
        /// </summary>
        /// <param name="path">Location of directory or file to recycle</param>
        /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
        private static bool Send(IEnumerable <string> paths, NativeMethods.FileOperationFlags flags)
        {
            try
            {
                // Generate a single buffer containing all the file names
                StringBuilder builder = new StringBuilder();
                foreach (string path in paths)
                {
                    builder.Append(path);
                    builder.Append('\0');
                }
                builder.Append('\0');                 // Double-termination signals the end of the buffer
                string buffer = builder.ToString();

                if (NativeMethods.IsWOW64Process())
                {
                    NativeMethods.SHFILEOPSTRUCT_x64 fs = new NativeMethods.SHFILEOPSTRUCT_x64();
                    fs.wFunc  = NativeMethods.FileOperationType.FO_DELETE;
                    fs.pFrom  = buffer;
                    fs.fFlags = NativeMethods.FileOperationFlags.FOF_ALLOWUNDO | flags;
                    NativeMethods.SHFileOperation_x64(ref fs);
                }
                else
                {
                    NativeMethods.SHFILEOPSTRUCT_x86 fs = new NativeMethods.SHFILEOPSTRUCT_x86();
                    fs.wFunc  = NativeMethods.FileOperationType.FO_DELETE;
                    fs.pFrom  = buffer;
                    fs.fFlags = NativeMethods.FileOperationFlags.FOF_ALLOWUNDO | flags;
                    NativeMethods.SHFileOperation_x86(ref fs);
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Send file to recycle bin
        /// </summary>
        /// <param name="path">Location of directory or file to recycle</param>
        /// <param name="flags">FileOperationFlags to add in addition to FOF_ALLOWUNDO</param>
        private static bool Send(IEnumerable<string> paths, NativeMethods.FileOperationFlags flags)
        {
            try
            {
                // Generate a single buffer containing all the file names
                StringBuilder builder = new StringBuilder();
                foreach (string path in paths)
                {
                    builder.Append(path);
                    builder.Append('\0');
                }
                builder.Append('\0'); // Double-termination signals the end of the buffer
                string buffer = builder.ToString();

                if (NativeMethods.IsWOW64Process())
                {
                    NativeMethods.SHFILEOPSTRUCT_x64 fs = new NativeMethods.SHFILEOPSTRUCT_x64();
                    fs.wFunc = NativeMethods.FileOperationType.FO_DELETE;
                    fs.pFrom = buffer;
                    fs.fFlags = NativeMethods.FileOperationFlags.FOF_ALLOWUNDO | flags;
                    NativeMethods.SHFileOperation_x64(ref fs);
                }
                else
                {
                    NativeMethods.SHFILEOPSTRUCT_x86 fs = new NativeMethods.SHFILEOPSTRUCT_x86();
                    fs.wFunc = NativeMethods.FileOperationType.FO_DELETE;
                    fs.pFrom = buffer;
                    fs.fFlags = NativeMethods.FileOperationFlags.FOF_ALLOWUNDO | flags;
                    NativeMethods.SHFileOperation_x86(ref fs);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }