Beispiel #1
0
        private static void CopyToFolder(Shell32.Folder srcFolder, Shell32.Folder dstFolder, bool copySubFolders, ref StringCollection resultList)
        {
            const string FileTypesNotToCopy = "aspx";
            const string FoldersNotToCopy   = "forms";

            foreach (Shell32.FolderItem item in srcFolder.Items())
            {
                if (item.IsLink)
                {
                    continue;
                }

                if (item.IsFolder && copySubFolders)
                {
                    Shell32.Folder     subFolder = (Shell32.Folder)item.GetFolder;
                    Shell32.FolderItem it        = dstFolder.ParseName(subFolder.Title);

                    // Only Copy the folders we want to copy
                    if (FoldersNotToCopy.Contains(subFolder.Title.ToLower()))
                    {
                        continue; //skip folder
                    }

                    // if destination folder doesn't exist create it
                    if (!(it != null && it.Type != null && it.Type != ""))
                    {
                        dstFolder.NewFolder(subFolder.Title, 0x414);
                    }

                    // We can now safely assume we have the folder, so we can copy it
                    it = dstFolder.ParseName(subFolder.Title);
                    CopyToFolder((Shell32.Folder)item.GetFolder, (Shell32.Folder)it.GetFolder, copySubFolders, ref resultList);

                    continue;
                }


                string fType = Path.GetExtension(item.Path);
                fType = fType.TrimStart('.');

                // Only Copy the files we want to copy
                if (FileTypesNotToCopy.Contains(fType.ToLower()))
                {
                    continue; //skip file
                }

                // Add to result set
                resultList.Add(item.Path);

                //check if this file already exists locally
                Shell32.FolderItem si = srcFolder.ParseName(item.Name);
                Shell32.FolderItem di = dstFolder.ParseName(item.Name);

                string tmpFile = Path.GetTempPath() + Path.GetFileName(item.Name);

                // if file already exists - we only copy over when newer
                if (di != null && di.Type != null && di.Type != "")
                {
                    // It looks like we first must copy the file over locally into a temp
                    // directory JUST to check the timestamp (modified date) ~very inefficient, but works
                    Shell32.ShellClass shell     = new Shell32.ShellClass();
                    Shell32.Folder     tmpFolder = shell.NameSpace(Path.GetTempPath());

                    if (File.Exists(tmpFile))   // Delete first otherwise Explorer will prompt
                    {
                        File.Delete(tmpFile);
                    }

                    tmpFolder.CopyHere(item, 0x414);
                    Shell32.FolderItem ti = tmpFolder.ParseName(item.Name);

                    // check timestamps of the files
                    if (ti.ModifyDate > di.ModifyDate)
                    {
                        File.Delete(di.Path);          // we can delete the old file
                        dstFolder.CopyHere(ti, 0x414); //it appears there is a microsoft bug 0x414 won't do anything
                    }

                    File.Delete(tmpFile);
                }
                else // just copy over
                {
                    dstFolder.CopyHere(item, 0x414);    //it appears there is a microsoft bug 0x414 won't do anything
                }
            }
        }