Airikr
Airikr

Reputation: 6436

Getting IOException was unhandled in C#

I have a function that allows the application to get the size of your hard disk in total.

        public static ulong GetLogicalDisk()
    {
        ulong num = 0L;
        string[] logicalDrives = Environment.GetLogicalDrives();
        for (int i = 0; i < logicalDrives.Length; i++)
        {
            System.IO.DriveInfo info = new System.IO.DriveInfo(logicalDrives[i].ToString());
            if (info.DriveType == System.IO.DriveType.Fixed)
            {
                num += (ulong)info.TotalSize; //Exception here.
                Console.WriteLine(info.TotalSize);
            }
        }
        Console.WriteLine(num);
        return num;
    }

When I trying to launch the application in C#, I get the following error message on num += (ulong)info.TotalSize;

IOException was unhandled

Can anyone here help me with this? What's the problem?

Upvotes: 0

Views: 770

Answers (2)

thumbmunkeys
thumbmunkeys

Reputation: 20764

My guess is you access a network share that isn't there or a CD ROM drive that is not inserted.

Use DriveInfo.IsReady to check if the drive is available, before you read the size

Upvotes: 1

RB.
RB.

Reputation: 37222

It might be trying to access a floppy disk drive or CD-ROM drive, but where no physical media is mounted.

From the docs:

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

Can you print out the Name of the drive before the exception occurs - I bet it's your A: drive...

Upvotes: 1

Related Questions