1

I have this small problem with the DriveInfo Class. I know that the error is specific to the "IsReady" Property but I just don't know how to define it..

namespace Csp.Test.ConsoleApp
{
    public class Program
    {
        public static void Main()
        {
            //Create the server object - You will need create a list of the server objects.
            Server server = new Server();

            //Get all drives information
            List<DriveInfo> driveList = DriveInfo.GetDrives().ToList<DriveInfo>();

            //Insert information of one server - You will need get information of all servers
            server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
            server.ServerName = string.Concat("Server ", driveList.Count);

            //Inserts information in the newServers object
            for (int i = 0; i < driveList.Count; i++)
            {
                ServerDrive serverDrives = new ServerDrive();

                //Put here all the information to obeject Server                
                serverDrives.DriveLabel = driveList[i].Name;
                serverDrives.TotalSpace = driveList[i].TotalSize;
                serverDrives.DriveLetter = driveList[i].VolumeLabel;
                serverDrives.FreeSpace = driveList[i].TotalFreeSpace;

                //      server.ListServerDrives.Add(serverDrives);
                server.ServerDrives.Add(serverDrives);
            }

            //Add the information to an SQL Database using Linq.
            DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver");
            //   db.Servers.InsertAllOnSubmit(server);
            db.Servers.InsertOnSubmit(server);
            db.SubmitChanges();
        }

Any help would be greatly appreciated.

Ghostyy
  • 77
  • 3
  • 10
  • What is your "small problem?" You've not described what behaviour your code is expected to have, or the behaviour that it actually *does* have. Also, why is this tagged "c" when the code appears to be C#? – Dan Puzey Aug 07 '12 at 08:52
  • As the title states the error is "Drive is not ready" and at the top of the general post I highlighted "IsReady" The code won't run as the drive is not ready yet I do not know how to implement "IsReady" into it.. – Ghostyy Aug 07 '12 at 08:58
  • @Ghostyy I suspect this happens for drives with removeable media, i.e. floppy and optical drives. Are you planning to run this code on each server? – Jodrell Aug 07 '12 at 09:05

1 Answers1

3

Change the following line:

List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x=>x.IsReady).ToList();

Note that you can still get an IOException if the drive state changes between getting the drives list and querying the DriveInfo, so it is best you use a try-catch when accessing the DriveInfo.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • is there a way to "wake up" a drive? Windows 8.1 puts my backup hard drive into a kind of sleepmode. It is visible in Windows Explorer but first time you access it you hear it loading. in C# my drive is "IsReady=false". – juFo Feb 05 '15 at 10:01
  • 1
    @juFo I haven't tried this, but you might be able to write a little helper method (e.g. WakeUpDrive) that accesses the drive (maybe writes an empty text file to the drive and deletes it immediately). You would do this in a try-catch. – Eren Ersönmez Feb 05 '15 at 13:19