Question

I am creating the file list of both source & target directory in target location, the requirement is based on the difference of these two files copy only the files that are new from last iteration.

For 1st iteration copy works, next iteration creates a blank diff [Difference] file is created, to handle this I have added if condition where I check length of file and copy them again. But the if condition is not working as per behavior, with length zero it goes to else statement and throws out error with blank file name :

Could not find a part of the path 'C:\interswitch\source\'.

Here's the code:

 string path = Path.Combine(target_dir, "Diff.txt");

        if (new FileInfo(path).Length == 0)

         {

            foreach (FileInfo fi in sourceinfo.GetFiles())

         {
            fi.CopyTo(Path.Combine(targetinfo.ToString(), fi.Name), true);
         }
       }        

        else

        foreach (string file in File.ReadLines(path))
        {

            {
                string sourceFile   = System.IO.Path.Combine(source_dir, file);
                string destFile     = System.IO.Path.Combine(target_dir, file);
                System.IO.File.Copy(sourceFile, destFile, true);
            }


        }
Was it helpful?

Solution

if (new FileInfo(path).Length == 0) does not produce 0 apparently, you have to fix that

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top