Пример #1
0
        public IEnumerable <StorageFileInfo> GetAllFiles(StorageFolderInfo folder)
        {
            var files = new List <StorageFileInfo>();

            foreach (var childFolder in folder.ChildFolders)
            {
                files.AddRange(GetAllFiles(childFolder));
            }

            files.AddRange(folder.Files);

            return(files);
        }
Пример #2
0
        internal StorageFolderInfo Convert(DirectoryInfo info, string root = null)
        {
            var folder =
                new StorageFolderInfo()
            {
                Name     = info.Name,
                FullPath = info.FullName,
            };

            folder.RootFolder = root;

            return(folder);
        }
Пример #3
0
        public void Traverse(StorageFolderInfo folder
                             , Action <StorageFolderInfo, StorageFileInfo> fileAction = null
                             , Action <StorageFolderInfo> folderAction = null)
        {
            for (int i = folder.Files.Count - 1; i >= 0; i--)
            {
                var file = folder.Files[i];
                fileAction?.Invoke(folder, file);
            }

            for (int i = folder.ChildFolders.Count - 1; i >= 0; i--)
            {
                var childFolder = folder.ChildFolders[i];
                Traverse(childFolder, fileAction, folderAction);
                folderAction?.Invoke(childFolder);
            }
        }
Пример #4
0
        public StorageFolderInfo CreateFolderCore(StorageFolderInfo folder, bool isTopOnly)
        {
            var current = new DirectoryInfo(folder.FullPath);

            foreach (var directory in current.GetDirectories())
            {
                var childFolder = Convert(directory, folder.RootFolder);
                childFolder.Parent = folder;
                folder.ChildFolders.Add(childFolder);

                if (!isTopOnly)
                {
                    CreateFolderCore(childFolder, isTopOnly);
                }
            }

            foreach (var file in current.GetFiles())
            {
                folder.Files.Add(Convert(file, folder.RootFolder));
            }

            return(folder);
        }
Пример #5
0
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="compareSourceFolder"></param>
        /// <param name="compareTargetFolder"></param>
        /// <param name="forceDeleteFiles"></param>
        public static void SyncFiles(
            StorageFolderInfo clearFolder,
            StorageFolderInfo compareFolder,
            IList <string> forceDeleteFiles
            )
        {
            var compareFiles    = _storage.GetAllFiles(compareFolder);
            var changeFileCount = 0;

            _storage.Traverse(clearFolder,
                              (folder, file) =>
            {
                var hasSameFile = compareFiles
                                  .Any((cmpFile) =>
                                       cmpFile.RelativePath == file.RelativePath &&
                                       cmpFile.LastWriteTime == file.LastWriteTime
                                       );

                //删除clearFolder里与compareFolder相同的文件
                if (hasSameFile)
                {
                    File.Delete(file.FullPath);
                    folder.Files.Remove(file);
                }
                else
                {
                    if (forceDeleteFiles.Contains(file.Name))
                    {
                        Console.WriteLine($"force delete file {file.RelativePath}");
                        File.Delete(file.FullPath);
                        return;
                    }

                    changeFileCount++;
                    Console.WriteLine($"change file {file.RelativePath}");

                    //将不相同的文件复制到compareFolder
                    var newPath   = compareFolder.RootFolder + file.RelativePath;
                    var newFolder = Path.GetDirectoryName(newPath);

                    if (!Directory.Exists(newFolder))
                    {
                        Directory.CreateDirectory(newFolder);
                    }

                    File.Copy(file.FullPath, newPath, true);
                }
            }
                              );
            Console.WriteLine($"total change file {changeFileCount}");

            //删除空文件夹
            _storage.Traverse(clearFolder,
                              folderAction: (folder) =>
            {
                if (
                    folder.Parent != null &&
                    folder.Files.Count == 0 &&
                    folder.ChildFolders.Count == 0)
                {
                    Directory.Delete(folder.FullPath);
                    folder.Parent.ChildFolders.Remove(folder);
                }
            }
                              );
        }