0

当我尝试使用 F11(Step Into)调试器调试我的代码时,我的代码产生了预期的结果。当我尝试在没有调试器(没有断点)的情况下运行代码时,我的代码中的循环产生了意外的结果;具体来说,循环部分只循环1次并终止,我在这里很困惑,有人对此有任何想法吗?下面是我提到的产生意外结果的循环。

public bool CopyFileAndFolder(string sourceFolder, string replacePath)
{
   bool result = false;
   try
   {
      foreach (string extractPath in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
      {
         string destFolder = extractPath.Replace(sourceFolder, replacePath);
         if (!Directory.Exists(destFolder))
            Directory.CreateDirectory(destFolder);
      }

      foreach (string extractFile in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
      {
          string destFile = extractFile.Replace(sourceFolder, replacePath);
          File.Copy(extractFile, destFile, true);
      }
          result = true;
   }
   catch (Exception)
   {
       result = false;
   }
   return result;
}

完整的代码,我用这个方法调用了上面的方法:

    private bool StartFileRollBackProcess() 
    {
        bool result = false;

        string backupFolder = Path.Combine(ConfigurationManager.AppSettings["BackupPath"], completeVersionNumber);
        string destBackUpFolder = Directory.GetParent(iisConf.PhysicalPath).FullName;

        try
        {
            DirectoryInfo folderToBeDelete = new DirectoryInfo(destBackUpFolder);
            folderToBeDelete.Delete(true);

            if (Directory.Exists(backupFolder))
            {
                Directory.CreateDirectory(destBackUpFolder);
                result = CopyFileAndFolder(backupFolder, destBackUpFolder);

                if (result)
                {
                    ErrorMsg = "Copy process Failed,Your File has rolled back to previous version";
                    IsErrorDetected = true;
                }
                else
                {
                    ErrorMsg = "copy got error";
                    IsErrorDetected = true;
                }

            }
        }
        catch (Exception)
        {
            ErrorMsg = "Error during roll up process";
            IsErrorDetected = true;
        }

        return result;
    }
4

0 回答 0