Help with DriveInfo. VolumeLabel exception please
-
Greetings Gurus, While enumerating the drives in my machine I am trying to get the output to print "No Label" if the VolumeLabel value is null / length = 0. When I run the app, after the C: and D: respond, I keep getting an exception "the device is not ready" if I use the "No Label" qualifier, but the code runs fine with that bit commented out. What syntax should I be using please?
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives) { if (d.VolumeLabel.Length == 0)//Exception occurs here d.VolumeLabel = "No Label"; string label = d.VolumeLabel; if (d.IsReady == true) { Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); } }
-
Greetings Gurus, While enumerating the drives in my machine I am trying to get the output to print "No Label" if the VolumeLabel value is null / length = 0. When I run the app, after the C: and D: respond, I keep getting an exception "the device is not ready" if I use the "No Label" qualifier, but the code runs fine with that bit commented out. What syntax should I be using please?
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives) { if (d.VolumeLabel.Length == 0)//Exception occurs here d.VolumeLabel = "No Label"; string label = d.VolumeLabel; if (d.IsReady == true) { Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); } }
My bad, order was wrong. Should read
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);if (d.IsReady == true) { string label = d.VolumeLabel; if (label.Length == 0) d.VolumeLabel = "No Label"; { Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); } }
}
-
Greetings Gurus, While enumerating the drives in my machine I am trying to get the output to print "No Label" if the VolumeLabel value is null / length = 0. When I run the app, after the C: and D: respond, I keep getting an exception "the device is not ready" if I use the "No Label" qualifier, but the code runs fine with that bit commented out. What syntax should I be using please?
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives) { if (d.VolumeLabel.Length == 0)//Exception occurs here d.VolumeLabel = "No Label"; string label = d.VolumeLabel; if (d.IsReady == true) { Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); } }
Of course you're getting that exception if you ask the volume label of a drive that doesn't have any media inserted like a DVD, CD, BlueRay drive for instance:
DriveInfo\[\] allDrives = DriveInfo.GetDrives(); long divisorForGiga = 1024 \* 1024 \* 1024; foreach (DriveInfo d in allDrives) { String label = d.VolumeLabel; if (d.IsReady == true) { if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) { // I'm not sure if you can assign to VolumeLabel in any case // You'll have to check and see if it works d.VolumeLabel = "No Label"; } Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); Console.WriteLine(d.AvailableFreeSpace / divisorForGiga); Console.WriteLine(d.TotalFreeSpace / divisorForGiga); Console.WriteLine(d.TotalSize / divisorForGiga); } else { Console.WriteLine("Drive {0}: is not ready! Maybe there's no media inserted.") } }
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
-
My bad, order was wrong. Should read
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);if (d.IsReady == true) { string label = d.VolumeLabel; if (label.Length == 0) d.VolumeLabel = "No Label"; { Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.AvailableFreeSpace / 1024000000); Console.WriteLine(d.TotalSize / 1024000000); } }
}
Try this instead:
string label = d.VolumeLabel; if (label.Length == 0) label = "No Label"; { Console.WriteLine(label);
What was the point of assigning to label if you weren't going to use it for anything other than a lenght test?
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Of course you're getting that exception if you ask the volume label of a drive that doesn't have any media inserted like a DVD, CD, BlueRay drive for instance:
DriveInfo\[\] allDrives = DriveInfo.GetDrives(); long divisorForGiga = 1024 \* 1024 \* 1024; foreach (DriveInfo d in allDrives) { String label = d.VolumeLabel; if (d.IsReady == true) { if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) { // I'm not sure if you can assign to VolumeLabel in any case // You'll have to check and see if it works d.VolumeLabel = "No Label"; } Console.WriteLine(d.Name); Console.WriteLine(d.DriveType); Console.WriteLine(d.VolumeLabel); Console.WriteLine(d.DriveFormat); Console.WriteLine(d.AvailableFreeSpace / divisorForGiga); Console.WriteLine(d.TotalFreeSpace / divisorForGiga); Console.WriteLine(d.TotalSize / divisorForGiga); } else { Console.WriteLine("Drive {0}: is not ready! Maybe there's no media inserted.") } }
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
I'm going to change your life (just like mine was a few years back...) :-D
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) {
Can be written as this:
if (string.IsNullOrEmpty(d.VolumeLabel)) {
-
I'm going to change your life (just like mine was a few years back...) :-D
if (d.VolumeLabel == null || d.VolumeLabel.Length == 0) {
Can be written as this:
if (string.IsNullOrEmpty(d.VolumeLabel)) {
And that nice new version
if (string.IsNullOrWhitespace(" "))
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility