Example #1
0
        static bool operate(FO func, string[] fileNames, string pTo, FOF flags, IntPtr handle)
        {
            SHFILEOPSTRUCT shell = new SHFILEOPSTRUCT();
            shell.hwnd = handle;
            shell.wFunc = func;
            shell.pFrom = string.Empty;

            foreach (string fileName in fileNames)
            {
                shell.pFrom += fileName + '\0';
            }

            shell.pFrom += '\0';
            shell.pTo = pTo;
            shell.fFlags = flags;
            shell.fAnyOperationsAborted = false;
            shell.hNameMappings = IntPtr.Zero;
            shell.lpszProgressTitle = string.Empty;
            return (Shell32.SHFileOperation(ref shell) == 0);
        }
Example #2
0
        public static bool Operate(FO func, string[] fileNames, string pTo, FOF flags, IntPtr handle)
        {
            SHFILEOPSTRUCT shell = new SHFILEOPSTRUCT
            {
                hwnd  = handle,
                wFunc = func,
                pFrom = null
            };

            var builder = new StringBuilder();

            foreach (string fileName in fileNames)
            {
                builder.AppendFormat("{0}{1}", fileName, '\0');
            }

            builder.Append('\0');
            shell.pFrom  = builder.ToString();
            shell.pTo    = pTo;
            shell.fFlags = flags;
            shell.fAnyOperationsAborted = false;
            shell.hNameMappings         = IntPtr.Zero;
            shell.lpszProgressTitle     = string.Empty;
            int result = Shell32.SHFileOperation(ref shell);

            if (result == 0)
            {
                return(true);
            }

            if (Shell32.GetSHFileOperationErrorMessage(result, out string message))
            {
                throw new IOException(message);
            }

            throw new IOException($"SHFileOperation エラーコード: {result:X2}");
        }