Top / JUNK / SHFileOperation

SHFileOperation のサンプル

API

    using System;
    using System.Runtime.InteropServices;

    static class NativeMethods
    {
        public enum FileFuncFlags : uint
        {
            FO_MOVE = 0x1,
            FO_COPY = 0x2,
            FO_DELETE = 0x3,
            FO_RENAME = 0x4
        }

        [Flags]
        public enum FILEOP_FLAGS : ushort
        {
            FOF_MULTIDESTFILES = 0x1,
            FOF_CONFIRMMOUSE = 0x2,
            FOF_SILENT = 0x4,
            FOF_RENAMEONCOLLISION = 0x8,
            FOF_NOCONFIRMATION = 0x10,
            FOF_WANTMAPPINGHANDLE = 0x20,
            FOF_ALLOWUNDO = 0x40,
            FOF_FILESONLY = 0x80,
            FOF_SIMPLEPROGRESS = 0x100,
            FOF_NOCONFIRMMKDIR = 0x200,
            FOF_NOERRORUI = 0x400,
            FOF_NOCOPYSECURITYATTRIBS = 0x800,
            FOF_NORECURSION = 0x1000,
            FOF_NO_CONNECTED_ELEMENTS = 0x2000,
            FOF_WANTNUKEWARNING = 0x4000,
            FOF_NORECURSEREPARSE = 0x8000
        }

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet = CharSet.Auto)]
        public struct SHFILEOPSTRUCT
        {
            public IntPtr hwnd;
            public FileFuncFlags wFunc;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pFrom;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pTo;
            public FILEOP_FLAGS fFlags;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fAnyOperationsAborted;
            public IntPtr hNameMappings;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszProgressTitle;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct SHFILEOPSTRUCT64
        {
            public IntPtr hwnd;
            public FileFuncFlags wFunc;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pFrom;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pTo;
            public FILEOP_FLAGS fFlags;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fAnyOperationsAborted;
            public IntPtr hNameMappings;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszProgressTitle;
        }

        [DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "SHFileOperation")]
        static extern int SHFileOperation32(ref SHFILEOPSTRUCT lpFileOp);

        [DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "SHFileOperation")]
        static extern int SHFileOperation64(ref SHFILEOPSTRUCT64 lpFileOp);

        public static int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp) {
            if (IntPtr.Size == 4) {
                return SHFileOperation32(ref lpFileOp);
            } else {
                var lpFileOp64 = new SHFILEOPSTRUCT64 {
                    hwnd = lpFileOp.hwnd,
                    wFunc = lpFileOp.wFunc,
                    pFrom = lpFileOp.pFrom,
                    pTo = lpFileOp.pTo,
                    fFlags = lpFileOp.fFlags,
                    fAnyOperationsAborted = lpFileOp.fAnyOperationsAborted,
                    hNameMappings = lpFileOp.hNameMappings,
                    lpszProgressTitle = lpFileOp.lpszProgressTitle
                };

                int result = SHFileOperation64(ref lpFileOp64);

                lpFileOp.fAnyOperationsAborted = lpFileOp64.fAnyOperationsAborted;
                return result;
            }
        }
    }

実行

    using System;
    using System.Diagnostics;

    static class Program
    {
        [STAThread]
        static void Main() {
            var fileOp = new NativeMethods.SHFILEOPSTRUCT {
                wFunc = NativeMethods.FileFuncFlags.FO_COPY,
                pFrom = "C:\\work\\*.*\0\0",
                pTo = "C:\\Work2\0\0",
                fFlags = NativeMethods.FILEOP_FLAGS.FOF_NOCONFIRMATION | NativeMethods.FILEOP_FLAGS.FOF_NOCONFIRMMKDIR,
                fAnyOperationsAborted = true,
                hNameMappings = IntPtr.Zero,
            };
            NativeMethods.SHFileOperation(ref fileOp);
            Debug.Print("{0}", fileOp.fAnyOperationsAborted);
        }
    }



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   最終更新のRSS
Last-modified: 2019-09-20 (金) 14:32:19 (33d)