Quote Originally Posted by kevininstructor View Post
So how does one get this code to work with
Option explicit = On
Option strict = On

There are many problems with them set to On that will not allow this code to compile.
Just do what Option Strict tells you to do to correct the errors I just turned it on and fixed them (and tidied up a couple of little things in the code) - see code below:

vb.net Code:
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.  
  5.     Private Const WM_DEVICECHANGE As Integer = &H219
  6.     Private Const DBT_DEVICEARRIVAL As Integer = &H8000
  7.     Private Const DBT_DEVTYP_VOLUME As Integer = &H2
  8.  
  9.     'Device information structure
  10.     Public Structure DEV_BROADCAST_HDR
  11.         Public dbch_size As Int32
  12.         Public dbch_devicetype As Int32
  13.         Public dbch_reserved As Int32
  14.     End Structure
  15.  
  16.     'Volume information Structure
  17.     Private Structure DEV_BROADCAST_VOLUME
  18.         Public dbcv_size As Int32
  19.         Public dbcv_devicetype As Int32
  20.         Public dbcv_reserved As Int32
  21.         Public dbcv_unitmask As Int32
  22.         Public dbcv_flags As Int16
  23.     End Structure
  24.  
  25.     'Function that gets the drive letter from the unit mask
  26.     Private Function GetDriveLetterFromMask(ByRef Unit As Int32) As Char
  27.         Dim i As Integer
  28.         For i = 0 To 25
  29.             If CBool(Unit And i) Then Exit For
  30.             Unit = Unit >> 1
  31.         Next
  32.         Return Chr(i + 1 + Asc("A"))
  33.     End Function
  34.  
  35.     'Override message processing to check for the DEVICECHANGE message
  36.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  37.         If m.Msg = WM_DEVICECHANGE Then
  38.             If CInt(m.WParam) = DBT_DEVICEARRIVAL Then
  39.                 Dim DeviceInfo As DEV_BROADCAST_HDR
  40.                 DeviceInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
  41.                 If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
  42.                     Dim Volume As DEV_BROADCAST_VOLUME
  43.                     Volume = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
  44.                     Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
  45.                     If IO.File.Exists(IO.Path.Combine(DriveLetter, "test.txt")) Then
  46.                         '<<<< The test file has been found >>>>
  47.                         MessageBox.Show("Found test file")
  48.                     Else
  49.                         '<<<< Test file has not been found >>>>
  50.                         MessageBox.Show("Could not find test file")
  51.  
  52.                     End If
  53.                 End If
  54.             End If
  55.         End If
  56.         MyBase.WndProc(m)
  57.     End Sub
  58.  
  59. End Class