0

When I try to debug my code with debugger with F11 (Step Into), my code produced the expected result. When I try to run the code without debugger(without break point), the looping in my code produced unexpected result; to be specific, the looping part only loop for 1 times and terminated, I am confused here, anyone have any idea about this? Below is the loop I mentioned which produced unexpected result.

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;
}

The complete code, i called the method above with this method:

    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;
    }
Soon Ee
  • 73
  • 12
  • 4
    That catch is a bit iffy; it's hiding the information about an exception. You should at least do `Trace.WriteLine(exception.Message);` – Matthew Watson Nov 16 '15 at 09:11
  • Could be that when you build your folder structure changes in your Debug/ or Release/ folder. Have a look at them while debugging if you are using folders in your output and make sure that they are indeed in your build data – Dave Nov 16 '15 at 09:13
  • i am aware of that, thank you for your advice, but the problem is i do not have any exception being thrown when my code is producing unexpected result, do you have any idea why is these happens? – Soon Ee Nov 16 '15 at 09:14
  • hi @Dave by build data you are referring to the output file generated when i build my project? – Soon Ee Nov 16 '15 at 09:18
  • Sorry guys, i will start to comment over here until a acceptable answer is found – Soon Ee Nov 16 '15 at 09:48
  • "the looping code" - you have two, which one? Do you debug in debug mode and not-debug in release mode? Do you debug in VS and otherwise run the application without VS? Is the configuration the same in both modes? Does it matter where you set the breakpoint? Do you show variables in a watch window which might have a side effect? – Thomas Weller Nov 16 '15 at 11:54
  • the top one CopyFileAndFolder() is the code part with issues,i debug in debug mode and debug in VS,for the breakpoint, if i set the break point at the function who called the method, the code works, if i didnt set the code produce wrong result. – Soon Ee Nov 17 '15 at 00:46

0 Answers0