Hello I don’t know if I am posting this in the correct forum so if not please move to where you feel is the most appropriate.
First off a short background of the project I am working on- I am developing a program that will be run off removable media (i.e. USB Flash drive). This will be a “virtual desktop” which you will be able to take with you and have the same “desktop” on any system.
As we know drive letters can change with each host system the drive is plugged into, I need to be able to determine the path for the flash drive and access a specific folder ( i.e. My Documents)
I have an picture box(representing the icon) placed on my form (form1) and when I click the icon I want to be able to open and view the a specific folder from the portable drive.
I need a click event that will determine a dynamic drive letter for a directory on the removable drive and then open that directory in an explorer window.
I have tried a few other code snippets that only provide the use with the drive letter ( but the drive must be plugged in while the program is running, in my case the program will run off the drive so it must be plugged in prior to the program running)
Also I have came across the option of using the getFloders() and listing the drives in a combo box but this is not practical for my case.
Neither of these have been able to solve my problem yet. Any ideas?
First off a short background of the project I am working on- I am developing a program that will be run off removable media (i.e. USB Flash drive). This will be a “virtual desktop” which you will be able to take with you and have the same “desktop” on any system.
As we know drive letters can change with each host system the drive is plugged into, I need to be able to determine the path for the flash drive and access a specific folder ( i.e. My Documents)
I have an picture box(representing the icon) placed on my form (form1) and when I click the icon I want to be able to open and view the a specific folder from the portable drive.
I need a click event that will determine a dynamic drive letter for a directory on the removable drive and then open that directory in an explorer window.
I have tried a few other code snippets that only provide the use with the drive letter ( but the drive must be plugged in while the program is running, in my case the program will run off the drive so it must be plugged in prior to the program running)
VB.NET:
Imports System.Management
Imports System.Threading
Public Class Form1
Private WithEvents m_MediaConnectWatcher As New ManagementEventWatcher
Public Sub StartDetection()
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")
m_MediaConnectWatcher = New ManagementEventWatcher
m_MediaConnectWatcher.Query = query2
m_MediaConnectWatcher.Options.Timeout = New TimeSpan(1000)
m_MediaConnectWatcher.Start()
End Sub
Private Sub Arrived(ByVal Sender As Object, ByVal E As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
Dim mbo, obj As ManagementBaseObject
mbo = CType(E.NewEvent, ManagementBaseObject)
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
'MsgBox(mbo.ClassPath.ClassName)
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj("InterfaceType").ToString.ToUpper = "USB" Then
'MessageBox.Show(obj("Caption") & Environment.NewLine & "Drive Letter (" & GetDriveLetterFromDisk(obj("Name") & ")"))
MessageBox.Show("Drive Letter " & GetDriveLetterFromDisk(obj("Name")))
Else
' MsgBox(obj("InterfaceType"))
End If
Case "__InstanceDeletionEvent"
If obj("InterfaceType").ToString.ToUpper = "USB" Then
Me.NotifyIcon1.Visible = True
Me.NotifyIcon1.BalloonTipTitle = "USB Device Disconnected"
Me.NotifyIcon1.BalloonTipText = obj("Caption").ToString & " has been disconnected."
Me.NotifyIcon1.ShowBalloonTip(200)
Else
' MsgBox(obj("InterfaceType"))
End If
Case Else
' MsgBox("nope: " & obj("Caption").ToString)
End Select
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_MediaConnectWatcher.Stop()
End Sub
Private Sub Form1_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
StartDetection()
End Sub
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
' MsgBox("Plug in a usb flash drive")
End Sub
Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""
' WMI queries use the "\" as an escape charcter
Name = Replace(Name, "\", "\\")
' First we map the Win32_DiskDrive instance with the association called
' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion
' instance with the assocation called Win32_LogicalDiskToPartition
oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()
oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk("Name") & ","
Next
Next
Return ans.Trim(","c)
End Function
End Class
Also I have came across the option of using the getFloders() and listing the drives in a combo box but this is not practical for my case.
VB.NET:
TreeView1.Nodes.Clear()
If ComboBox1.Text = "C" Then
GetFolders(Nothing, My.Computer.FileSystem.SpecialDirectories.MyMusic, TreeView1)
Else
Try
GetFolders(Nothing, ComboBox1.Text & ":\music", TreeView1)
End Try
Neither of these have been able to solve my problem yet. Any ideas?