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. USB (removable) drives in listbox

USB (removable) drives in listbox

Scheduled Pinned Locked Moved C#
question
8 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.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #1

    I tried this: System.IO.DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady == true & d.DriveType == "Removable") { listBox1.Items.Add(d.Name); } } ...and it did not work. I'm getting a headache. How do I accomplish what I'm trying to do?

    N M A 3 Replies Last reply
    0
    • S simplicitylabs

      I tried this: System.IO.DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady == true & d.DriveType == "Removable") { listBox1.Items.Add(d.Name); } } ...and it did not work. I'm getting a headache. How do I accomplish what I'm trying to do?

      N Offline
      N Offline
      nateraaaa
      wrote on last edited by
      #2

      What type of error did you receive? Your if statement is incorrect. Your if statement should contain && not &. This may be a typo but add another & and see what happens. Nathan

      1 Reply Last reply
      0
      • S simplicitylabs

        I tried this: System.IO.DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady == true & d.DriveType == "Removable") { listBox1.Items.Add(d.Name); } } ...and it did not work. I'm getting a headache. How do I accomplish what I'm trying to do?

        M Offline
        M Offline
        MatrixCoder
        wrote on last edited by
        #3

        Try replacing d.DriveType == "Removable" with d.DriveType == System.IO.DriveType.Removable.


        Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

        S 1 Reply Last reply
        0
        • M MatrixCoder

          Try replacing d.DriveType == "Removable" with d.DriveType == System.IO.DriveType.Removable.


          Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

          S Offline
          S Offline
          simplicitylabs
          wrote on last edited by
          #4

          Thanks a bunch, guys. The world's easiest way to list all USB drives on the system...This worked:

                  System.IO.DriveInfo\[\] allDrives = System.IO.DriveInfo.GetDrives();
          
                  foreach (System.IO.DriveInfo d in allDrives)
                  {
                      if (d.IsReady == true && d.DriveType == System.IO.DriveType.Removable)
                      {
                          listBox1.Items.Add(d.Name);
                      }
                  }
          

          Ok. Now that we have that worked out. Let me say this: I have this code in "form1.designer.cs", which I know is incorrect. How do I get it where it belongs (in form1.cs) and have it work right? I put it in there previously, and it didn't work.

          M 1 Reply Last reply
          0
          • S simplicitylabs

            Thanks a bunch, guys. The world's easiest way to list all USB drives on the system...This worked:

                    System.IO.DriveInfo\[\] allDrives = System.IO.DriveInfo.GetDrives();
            
                    foreach (System.IO.DriveInfo d in allDrives)
                    {
                        if (d.IsReady == true && d.DriveType == System.IO.DriveType.Removable)
                        {
                            listBox1.Items.Add(d.Name);
                        }
                    }
            

            Ok. Now that we have that worked out. Let me say this: I have this code in "form1.designer.cs", which I know is incorrect. How do I get it where it belongs (in form1.cs) and have it work right? I put it in there previously, and it didn't work.

            M Offline
            M Offline
            MatrixCoder
            wrote on last edited by
            #5

            simplicitylabs wrote:

            I put it in there previously, and it didn't work.

            It works fine for me. Was there an error message?


            Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

            S 1 Reply Last reply
            0
            • M MatrixCoder

              simplicitylabs wrote:

              I put it in there previously, and it didn't work.

              It works fine for me. Was there an error message?


              Trinity: Neo... nobody has ever done this before. Neo: That's why it's going to work.

              S Offline
              S Offline
              simplicitylabs
              wrote on last edited by
              #6

              Oh, it compiles and runs nicely. However, because I have it in form1.designer.cs, which is automatically generated by the designer, and not form1.cs, I get a warning and cannot view the form in design mode: -------The warning------- The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. ------------------------- I'm using Visual Studio 2005.

              S 1 Reply Last reply
              0
              • S simplicitylabs

                Oh, it compiles and runs nicely. However, because I have it in form1.designer.cs, which is automatically generated by the designer, and not form1.cs, I get a warning and cannot view the form in design mode: -------The warning------- The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. ------------------------- I'm using Visual Studio 2005.

                S Offline
                S Offline
                simplicitylabs
                wrote on last edited by
                #7

                I got it. Thank you for your help, everyone. I'm sure I'll be back with more questions soon.

                1 Reply Last reply
                0
                • S simplicitylabs

                  I tried this: System.IO.DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { if (d.IsReady == true & d.DriveType == "Removable") { listBox1.Items.Add(d.Name); } } ...and it did not work. I'm getting a headache. How do I accomplish what I'm trying to do?

                  A Offline
                  A Offline
                  AFSEKI
                  wrote on last edited by
                  #8

                  Have a look at: http://icsharpcode.net/OpenSource/SharpUSBLib/default.aspx

                  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