Results 1 to 4 of 4

Thread: Code to detect USB drive in VB.NET?

  1. #1
    Join Date
    Jan 2009
    Posts
    38

    Code to detect USB drive in VB.NET?

    Hi,

    I know this is possible but don't know how to code this?

    If anyone knows how to detect the USB in VB.NET application then it will be helpful to me or please direct me.

  2. #2
    Join Date
    Jan 2009
    Posts
    36

    Re: Code to detect USB drive in VB.NET?

    It sounds like you want someone to do all the work for you. Put a Button ,Textbox and a Listbox on a form and use the following:

    Code:
    Imports System.Reflection
    
    Imports System.IO
    
    Public Class Form1
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    ' Find the first removable storage device and make this the initial
    
    ' directory if it exists
    
    Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()
    
    Dim d As IO.DriveInfo
    
    For Each d In allDrives
    
    If d.IsReady = True AndAlso d.DriveType = IO.DriveType.Removable Then
    
    ListBox1.Items.Add(d.VolumeLabel).ToString()
    
    If IO.DriveType.Removable Then
    
    TextBox1.Text = d.AvailableFreeSpace
    
    End If
    
    Else
    
    If d.IsReady = True And Not d.DriveType = DriveType.Removable Then
    
    ListBox1.Items.Add(d.RootDirectory).ToString()
    
    TextBox1.Text = d.AvailableFreeSpace
    
    End If
    
    End If
    
    Next
    
    End Sub
    
    End Class
    It's up to you to figure out how to get more info from IO.DriveInfo etc. Using System.Reflection.

  3. #3
    Join Date
    Jan 2009
    Posts
    19

    Re: Code to detect USB drive in VB.NET?

    you are going to have to use P/Invoke and RegisterDeviceNotification.
    Here's a link to some code by Wei Mao at MS.

    http://www.dotnet247.com/247referenc...32/164968.aspx

  4. #4
    Join Date
    May 2008
    Posts
    27

    Re: Code to detect USB drive in VB.NET?

    You'll need WMI for this. It will allow you to enumerate all removable
    devices in the system and query their properties such as label, serial
    number etc. If you want notification of when a drive is connected, WMI
    should be able to handle that as well.

    There is an excellent WMI plugin for the server explorer in VS.NET available
    free on the MS website, it will assist in wiring up event code for WMI
    classes.

Similar Threads

  1. Folder not detect by pen drive
    By Dakarai in forum Portable Devices
    Replies: 4
    Last Post: 11-09-2010, 09:31 PM
  2. Unable to detect DVD drive
    By Harsh01 in forum Hardware Peripherals
    Replies: 7
    Last Post: 30-03-2010, 02:30 PM
  3. Cannot detect USB Pen Drive & USB Mouse
    By merf in forum Hardware Peripherals
    Replies: 3
    Last Post: 18-03-2009, 05:43 AM
  4. XP does not detect my hard drive
    By Woodrow in forum Hardware Peripherals
    Replies: 4
    Last Post: 29-01-2009, 09:52 AM
  5. Computer does not detect CD-RW drive
    By Icarusul in forum Hardware Peripherals
    Replies: 2
    Last Post: 30-07-2008, 02:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,483,417.33818 seconds with 17 queries