递归传输和分解文件/文件夹

时间:2014-12-01 21:16:16

标签: c#

我写了一个应用程序,用于浏览非常大的文件共享,当它将它们复制到新位置时,在这种情况下是另一个HD,它会将正在复制的内容分解为包含4,500或更少项目的文件夹(要在线同步到sharepoint的库) )。补偿长文件路径。这是我一前一后工作的函数的代码示例,它接收源和目标然后执行上述复制:

编辑:

好吧我发现了一个解决方案,下面这个方法按照我的意愿运行,它真的很慢,因为你可以想象看看我如何计算子项目,因为它正在运行,所以如果有人有更好/更快的解决方案问题请随意发布。否则,非常感谢大家的帮助,非常感谢。这是我目前的代码:

#region Recursive_Copy
    public static List<string> OutOfReachAreas = new List<string>();
    private static List<FileInfo> Overflow = new List<FileInfo>();
    private static List<string> AccessDeniedList = new List<string>();
    private static bool FirstTime = true;
    private static int LibraryCount { get; set; }
    private static bool ToSwith = false;
    private static int CountLimit = 4250;
    public static void RecursiveCopy(string SourceDir, string DestDir)
    {
        if (!FirstTime)
        {
            LibraryCount =
            ((Directory.GetDirectories((Program.DestinationContainer + "\\" + "Library" + Program.LibraryCounter.ToString()), "*", SearchOption.AllDirectories).Count())
           + (Directory.GetFiles((Program.DestinationContainer + "\\" + "Library" + Program.LibraryCounter.ToString()), "*", SearchOption.AllDirectories).Count()));
        }

        if (LibraryCount <= CountLimit && !FirstTime)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(SourceDir);
                DirectoryInfo[] dirs = dir.GetDirectories();
                FileInfo[] files = dir.GetFiles();
                string CurrentLibrary = "Library" + Program.LibraryCounter.ToString();

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

                if (Overflow.Count() != 0)
                {
                    foreach (var OverflowInst in Overflow)
                    {
                        string NewestLibrary = Program.DestinationContainer + "\\" + "Library" + Program.LibraryCounter + "\\" + OverflowInst.Name;
                        if (!File.Exists(NewestLibrary))
                        {
                            OverflowInst.CopyTo(NewestLibrary, false);
                        }
                    }
                    Overflow.Clear();
                }

                foreach (var file in files)
                {
                    try
                    {
                        DirectoryInfo TestPath = new DirectoryInfo(Program.DestinationContainer + "\\" + CurrentLibrary);
                        int TestPathCount = (TestPath.GetDirectories("*", SearchOption.AllDirectories).Count()) + (TestPath.GetFiles("*", SearchOption.AllDirectories).Count());
                        if (TestPathCount <= CountLimit)
                        {
                            string temppath = Path.Combine(DestDir, file.Name);
                            DirectoryInfo TestTemp = new DirectoryInfo(temppath);
                            if (!TestTemp.Exists && !AccessDeniedList.Contains(file.Name))
                            {
                                file.CopyTo(temppath, true);
                            }
                        }
                        else
                        {
                            FileInfo OverflowToAdd = new FileInfo(file.FullName);
                            Overflow.Add(OverflowToAdd);
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        AccessDeniedList.Add(file.Name);
                    }
                }

                foreach (var subDir in dirs)
                {
                    DirectoryInfo TestPath = new DirectoryInfo(Program.DestinationContainer + "\\" + CurrentLibrary);
                    int TestPathCount = (TestPath.GetDirectories("*", SearchOption.AllDirectories).Count()) + (TestPath.GetFiles("*", SearchOption.AllDirectories).Count());
                    if (TestPathCount <= CountLimit)
                    {
                        if (ToSwith)
                        {
                            ToSwith = false;
                            string PathToSwitch = Program.DestinationContainer + "\\" + "Library" + Program.LibraryCounter.ToString();
                            RecursiveCopy(SourceDir, PathToSwitch);
                        }
                        string temppath = Path.Combine(DestDir, subDir.Name);
                        RecursiveCopy(subDir.FullName, temppath);
                    }
                    else
                    {
                        DirectoryInfo CurrentDir = new DirectoryInfo(DestDir);
                        RecursiveCopy(SourceDir, (Program.DestinationContainer + "\\" + "Library" + Program.LibraryCounter.ToString()));
                    }
                }
            }
            catch (PathTooLongException)
            {
                DirectoryInfo DirInst = new DirectoryInfo(SourceDir);
                OutOfReachAreas.Add(DirInst.FullName);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nError During Copying:\n" + e.Message);
            }
        }
        else
        {
            ++Program.LibraryCounter;
            FirstTime = false;
            ToSwith = true;
            string LibraryToMake = Program.DestinationContainer + "\\" + "Library" + (Program.LibraryCounter.ToString());
            Console.WriteLine("Building Library" + (Program.LibraryCounter.ToString()) + "...");
            Directory.CreateDirectory(LibraryToMake);
            RecursiveCopy(SourceDir, LibraryToMake);
        }
    }
    #endregion

1 个答案:

答案 0 :(得分:0)

就像chais在评论中所说,你需要子目录搜索是递归的。

MS示例为EACH子目录调用自身,因此它遍历整个目录树。你可以通过替换来做类似的事情:

    foreach (var dir in FolderList1)
    {
        Destination = Destination + "\\" + dir.Name;
        CopyItems(dir, Destination);
    }

    foreach (var dir in FolderList1)
    {
        Destination = Destination + "\\" + dir.Name;
        CopyItems(dir, Destination);
        foreach (Directory subDir in dir.GetDirectories())
        {
            BreakMain(subDir, Destination);
        }
    }

你还有一些其他的事情需要修复才能转移到这个递归设计,但这就是MS例子的主旨。