Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help with DriveInfo. VolumeLabel exception please

Help with DriveInfo. VolumeLabel exception please

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CCodeNewbie
    wrote on last edited by
    #1

    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);
            }
        }
    
    C M 2 Replies Last reply
    0
    • C CCodeNewbie

      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);
              }
          }
      
      C Offline
      C Offline
      CCodeNewbie
      wrote on last edited by
      #2

      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);
          }
      }
      

      }

      P 1 Reply Last reply
      0
      • C CCodeNewbie

        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);
                }
            }
        
        M Offline
        M Offline
        Manfred Rudolf Bihy
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • C CCodeNewbie

          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);
              }
          }
          

          }

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Manfred Rudolf Bihy

            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

            M Offline
            M Offline
            Matt Meyer
            wrote on last edited by
            #5

            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)) {

            P 1 Reply Last reply
            0
            • M Matt Meyer

              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)) {

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups