예제 #1
0
    public static int CopyFiles()
    {
        // set up our file operation structure
        SHFILEOPSTRUCT sh;

        sh.hwnd  = IntPtr.Zero;
        sh.WFunc = FO_Func.FO_COPY;
        sh.PFrom = @"C:\temp\f1\a.txt" + Constants.vbNullChar + @"C:\temp\f1\b.txt" + Constants.vbNullChar +
                   Constants.vbNullChar;
        sh.fFlags = (ushort)FILEOP_FLAGS_ENUM.FOF_ALLOWUNDO
                    | (ushort)FILEOP_FLAGS_ENUM.FOF_MULTIDESTFILES
                    | (ushort)FILEOP_FLAGS_ENUM.FOF_WANTMAPPINGHANDLE;
        sh.FAnyOperationsAborted = false;
        sh.HNameMappings         = IntPtr.Zero;
        sh.LpszProgressTitle     = null;
        sh.pTO = @"C:\temp\a.txt" + Constants.vbNullChar + @"C:\temp\b.txt" + Constants.vbNullChar +
                 Constants.vbNullChar;

        int ret = SHFileOperation(ref sh);

        if (sh.HNameMappings != IntPtr.Zero)
        {
            // Copy from Sh.HNameMappings to HANDLETOMAPPINGS
            HANDLETOMAPPINGS mappings =
                (HANDLETOMAPPINGS)Marshal.PtrToStructure(sh.HNameMappings, typeof(HANDLETOMAPPINGS));
            // initialize the pointer for reading at the position of the mappings.LpSHNameMapping
            IntPtr ptr = mappings.LpSHNameMapping;
            for (int I = 0; I < (int)mappings.UNumberOfMappings; I++)
            {
                // read from the pointer
                // copying to SHNAMEMAPPING
                SHNAMEMAPPING mapping = (SHNAMEMAPPING)Marshal.PtrToStructure(ptr, typeof(SHNAMEMAPPING));
                // create file info from mapping
                // something we can get at
                FileInfo fileNew = new FileInfo(mapping.PszNewPath);
                Console.WriteLine("new file name: {0}", fileNew.Name);
                // advance by the size of the structure to the next SHNAMEMAPPING
                ptr = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(typeof(SHNAMEMAPPING)));
            }
        }
        return(ret);
    }
예제 #2
0
 private static extern void SHFreeNameMappings(SHNAMEMAPPING IntPtr);