图书馆没有正确复制

时间:2014-12-17 00:14:24

标签: c# recursion

我有一套递归方法,用于将大型文件共享复制到新目标,并且在复制时,会跟踪它创建的目标文件夹中有多少项,以便将源文件共享分成更小的库。该错误分为两个阶段:

首先

在库之间切换的过程中(例如:停止复制到library1并开始复制到library2)它会在切换之前将不需要的文件和文件夹复制到第一个库。

第二

它将前几个文件复制到第二个库然后停止,当我休息并查看局部变量时,它看起来好像在运行非托管代码,因此我不能当它被卡住时,看到不同变量的确切值。

我在寻找什么:

我正在寻找一种解决此错误的方法,并在文件到达一定数量的项目(文件和文件夹)时停止将文件复制到第一个库,并在执行溢出列表时开始复制到下一个库在方法中创建实例。

递归方法区域:

#region Recursive_Copy
    private static List<string> AccessDeniedList = new List<string>();
    public static void RecursiveCopy(string SourceDir, string DestDir, List<string> OverFlow, bool ToSwitch, bool FirstTime)
    {
        OverFlow = OFlowChecker(OverFlow);
        CopyData Data = new CopyData();
        string CurrentLibrary = CopyData.CurrentWorkingLibrary;
        int LibCount = Data.LibraryCount();

        if (FirstTime)
        {
            NewLibrary(SourceDir, OverFlow, ToSwitch, FirstTime);
        }
        else if (ToSwitch || LibCount >= CopyData.CountLimit)
        {
            NewLibrary(SourceDir, OverFlow, ToSwitch, FirstTime);
        }
        else if (!ToSwitch && LibCount <= CopyData.CountLimit)
        {
            LibCount = Data.LibraryCount();
            if (CurrentLibrary == @"N:\atf\Library3" || CurrentLibrary == @"N:\atf\Library7")
            {
                // Break Point
                Debugger.Break();
            }
            try
            {
                CopyData OFlow = new CopyData();
                DirectoryInfo dir = new DirectoryInfo(SourceDir);
                DirectoryInfo[] dirs = dir.GetDirectories();
                FileInfo[] files = dir.GetFiles();
                if (!Directory.Exists(DestDir))
                {
                    Directory.CreateDirectory(DestDir);
                }

                if (files.Count() > 0)
                {
                    EnumFiles(files, DestDir, dir.FullName, ref OverFlow, ref LibCount);
                }

                if (dirs.Count() > 0)
                {
                    EnumFolders(dirs, SourceDir, DestDir, CurrentLibrary, LibCount, OverFlow);
                }
            }
            catch (PathTooLongException)
            {
                // Break Point
                DirectoryInfo DirInst = new DirectoryInfo(SourceDir);
                CopyData.OutOfReachAreas.Add(DirInst.FullName);
            }
            catch (Exception e)
            {
                // Break Point
                Console.WriteLine("\nError During Copying:\n" + e.Message + "\nSource:\n" + e.Source);
            }
        }
        else
        {
            Console.WriteLine("There Was A LibraryMisDirectException. This Error Is Common But If It Persists Contact Your Developer.");
            // Break Point
            RecursiveCopy(SourceDir, DestDir, OverFlow, false, false);
        }
    }

    private static void EnumFiles(FileInfo[] files, string DestDir, string CurrentLibrary, ref List<string> OverFlow, ref int RunningCount)
    {
        CopyData data = new CopyData();
        foreach (var file in files)
        {
            if (!(RunningCount + 1 >= CopyData.CountLimit))
            {
                try
                {
                    string temppath = Path.Combine(DestDir, file.Name);
                    if (!(new FileInfo(temppath).Exists && AccessDeniedList.Contains(file.Name)))
                    {
                        RunningCount++;
                        file.CopyTo(temppath, true);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    AccessDeniedList.Add(file.Name);
                    Console.WriteLine("\nAccess Was Denied Accessing:\n" + file.Name + "\nIn:\n" + file.Directory.FullName);
                }
            }
            else
            {
                OverFlow.Add(file.FullName);
            }
        }
    }

    private static void EnumFolders(DirectoryInfo[] directories, string Source, string Destination, string CurrentLibrary, int CurrentCount, List<string> OverFlow)
    {
        CopyData data = new CopyData();
        foreach (var subDir in directories)
        {
            CurrentLibrary = CopyData.CurrentWorkingLibrary;
            if ((data.LibraryCount() + 1 + subDir.GetFiles().Count()) >= CopyData.CountLimit)
            {
                // Break Point
                RecursiveCopy(subDir.FullName, Destination, OverFlow, true, false);
            }
            else
            {
                string temppath = Path.Combine(Destination, subDir.Name);
                RecursiveCopy(subDir.FullName, temppath, OverFlow, false, false);
            }
        }
    }
    #endregion

CopyData类:

#region Public Data
    public static string SourceDirectory { get; set; }

    public static string DestinationContainer { get; set; }

    public static string CurrentWorkingLibrary { get; set; }

    public static int LibraryCounter = 0;

    public static List<string> OutOfReachAreas = new List<string>();

    public List<string> OverFlowFiles = new List<string>();

    public static int CountLimit = 1000;

    public int LibraryCount()
    {
        if (CurrentWorkingLibrary != null)
        {
            try
            {
                DirectoryInfo LibDir = new DirectoryInfo(CurrentWorkingLibrary);
                int files = LibDir.GetFiles("*", SearchOption.AllDirectories).Count();
                int folders = LibDir.GetDirectories("*", SearchOption.AllDirectories).Count();
                int sum = files + folders;
                return sum;
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                return 0;
            }
        }
        else
        {
            CurrentWorkingLibrary = DestinationContainer + "\\Library" + LibraryCounter.ToString();
            return 0;
        }
    }
    #endregion

备注:

我已经尝试逐步完成这个过程,你会看到我在代码中标出了断点,我已经广泛地谷歌并且已经调试了这个问题大约两个星期了。所以任何帮助都将不胜感激。

0 个答案:

没有答案