Strange problem with the File.Move command

advertisements

I have encountered a strange problem when using the File.Move command. The Programm actually moves and renames the file, but then throws me an exception that the sourcefile is not found; - what is expected because the File was moved.

The Program works fine if i catch the Exception but i'm wondering why i get these exception.

My Code:

  foreach (string str in CPM.prot.FKFinishedBad)
  {
        try
        {
          string dir = System.Configuration.ConfigurationSettings.AppSettings["ResultDir"] + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + "_Bearbeitete Protokolle";
        if (!Directory.Exists(dir))
        {
          Directory.CreateDirectory(dir);
        }
          File.Move(System.Configuration.ConfigurationSettings.AppSettings["ResultDir"] + "\\" +  str + "_" + CPM.LastJob + ".txt", dir + "\\" + "\\" + str + "_" + CPM.LastJob + "_Nachproduziert" + ".txt");
        }
        catch (Exception e)
        {
        }
    }


Are you sure all of your files exist? It might happen that one of them is missing (which explains the exception), while the others are processed correctly. you can also check them before the move with File.Exists.

Also, be careful when using empty catch blocks, they can cause a lot of headaches when debugging.