Count to infinity and beyond, not ready

So, I create a reusable class that will handle recursion and copy the directory one to another directory. Folder structure:

  • Root ( D:\Arrigotti)
  • Source ( C:\inetpub\wwwroot)
  • Destination ( D:\Arrigotti\Backup)
  • Archive ( D:\Arrigotti\Archive)

These are critical areas, for example code I'm going to not use some checks / errors for simplicity.

public static class FileSystem
{
     public static void CopyDirectory(string source, string destination)
     {
           DirectoryInfo directory = new DirectoryInfo(source);
           DirectoryInfo[] directories = directory.GetDirectories();
           foreach(DirectoryInfo dir in directories)
           {
                 Console.WriteLine(@"Found Directory: {0}", dir.FullName);
                 if(Directory.Exists(Path.Combine(destination, dir.Name)))
                 {
                       Console.WriteLine(@"Attempting to write directory...");
                       Directory.CreateDirectory(Path.Combine(destination, dir.Name));
                       Console.WriteLine(@"Created Directory: {0}", dir.Name);
                 }

                 CopyDirectory(dir.FullName, Path.Combine(destination, dir.Name));
                 FileInfo[] files = dir.GetFiles();
                 foreach(FileInfo file in files)
                 {
                       Console.WriteLine(@"Found: {0}", file.FullName);
                       Console.WriteLine(@"Attempting to copy...");
                       file.CopyTo(Path.Combine(destination, file.Name), true);
                 }
           }
     }
}

I think this part is pretty accurate and working. However, my problem is related to my challenge.

public static class Backup
{
     public static void Save()
     {
           string[] drives = Directory.GetLogicalDrives();
           foreach(string drive in drives)
           {
                DriveInfo diagnose = new DriveInfo(drive);
                if(diagnose.VolumeLabel == @"Backup" && diagnose.DriveType == DriveType.Fixed)
                {
                    CopyDirectory(
                          ConfigurationManager.AppSettings[@"Source"],
                          ConfigurationManager.AppSettings[@"Destination"]);
                }
            }
      }
}

Currently, the code works like a smooth swim, it completes five of the hundred website directories that it copies, then it accidentally throws an exception (for simplicity, I left the error handling, but this is an error.)

IOException: device is not ready.

, , .

+4
2

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(source,destination,true);

. , . , .VolumeLabel DirectoryInfo. MSDN: http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel(v = vs .110).aspx

, , , . ,

IOException - An I/O error occurred (for example, a disk error or a drive was not ready).

, , diagnose.isReady

public static void Save()
{
    string[] drives = Directory.GetLogicalDrives();
    foreach(string drive in drives)
    {
        DriveInfo diagnose = new DriveInfo(drive);
        if(diagnose.IsReady && diagnose.VolumeLabel == @"Backup" && diagnose.DriveType == DriveType.Fixed)
              ^^Make sure the drive is ready before examining properties
        {
            CopyDirectory(
                ConfigurationManager.AppSettings[@"Source"],
                ConfigurationManager.AppSettings[@"Destination"]);
        }
    }
 }
+2

Source: https://habr.com/ru/post/1544375/


All Articles