type definition problem

kaveh rahimi 61 Reputation points
2021-05-06T18:54:25.92+00:00

Hi, I receive "Type 'managementeventwatcher' is not defined" error in compilation.
My code is as below:
Imports System.Runtime.InteropServices
Imports System
Imports System.management
'Imports System.ManagementEventWatcher
Namespace monitordrives

End Namespace

Module Program
Class program
Public Enum EventType
Inserted = 2
Removed = 3
End Enum

    Sub Main()
        Dim watcher As ManagementEventWatcher = New ManagementEventWatcher()
        Dim query As WqlEventQuery = New WqlEventQuery()
        AddHandler watcher.EventArrived, AddressOf watcher_EventArrived

        watcher.Query = query
        watcher.Start()
        Console.ReadKey()

    End Sub





    Private Sub watcher_EventArrived(ByRef sender As Object, ByRef e As EventArrivedEventArgs)

        Dim driveName As String = e.NewEvent.Properties("DriveName").Value.ToString()
        Dim eventType As EventType = CType((Convert.ToInt16(e.NewEvent.Properties("EventType").Value)), EventType)
        Dim eventName As String = [Enum].GetName(GetType(EventType), eventType)
        Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName)
    End Sub
    'Sub Main()
    '   Console.WriteLine("Port Connected")
    'End Sub
    'Public Class socket
    'Dim usbdevice As socket = New socket()
    'Event usbarrival(ByVal iev As Integer)
    '   Sub usbdevice_usbarrival()
    '        AddHandler usbdevice.usbarrival, AddressOf Me.usbdevice_usbarrival

    '  End Sub
    'End Class
End Class

End Module
This error is received five times for other parameters anywhere I used As 'Type'.
Please help me.
Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2021-05-06T21:35:18.17+00:00

    Open the References for the project in the Solution
    Explorer and add a checkmark next to System.Management.

    • Wayne
    1 person found this answer helpful.

  2. WayneAKing 4,921 Reputation points
    2021-05-07T17:46:56.957+00:00

    You shouldn't have to copy anything. How did you
    look for System.Management? Did you open the
    References by right-clicking on that node in the
    project tree in the Solution Explorer window?

    Then choose "Add Reference" from the menu.

    Scroll down the Framework list and you should find
    System.Management.

    94770-system-management.jpg

    • Wayne
    0 comments No comments

  3. Karen Payne MVP 35,196 Reputation points
    2021-05-07T18:40:31.303+00:00

    If you can't find System.Management then perhaps the project is .NET Core (or regular .NET Framework) and if so add the reference via NuGet.

    Link to NuGet package.

    94828-figure1.png

    94903-figure3.png

    0 comments No comments

  4. kaveh rahimi 61 Reputation points
    2021-05-08T12:37:30.02+00:00

    Hi ,thanks a part of my problem is resolved.Now I have event 'load' can not be found error and 'msgbox' is not declared error.this is the line concerns my first error:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    and the second error:
    MsgBox(ex.Message)
    Which libraries I have to install?
    Please help me.
    Thank you very much

    0 comments No comments

  5. Karen Payne MVP 35,196 Reputation points
    2021-05-08T12:51:31.057+00:00

    No additional libraries are needed. You can try using MessageBox.Show instead and would recommend using Form Shown event rather than Form Load.