Hi .I am second year BSc(I.T). I am learning VB.net programming language. I want to write code to detect USB drive. I tried various codes but none of them worked out. If you are having any suggestions then reply me. It can helpful to me.
Hi .I am second year BSc(I.T). I am learning VB.net programming language. I want to write code to detect USB drive. I tried various codes but none of them worked out. If you are having any suggestions then reply me. It can helpful to me.
You will require WMI for this. It will give you permit to specify all removable devices in the system and query their properties like label, serial number etc. If you would like warning of when a drive is connected, WMI should be able to handle that also. There is an outstanding WMI plug-in for the server explorer in VS.NET available free on the Microsoft website; it will help in wiring up event code for WMI classes.
Try to understand following code which is helpful 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
Bookmarks