question

kavehrahimi-5744 avatar image
0 Votes"
kavehrahimi-5744 asked XingyuZhao-MSFT commented

type definition problem

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

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered XingyuZhao-MSFT commented

Given a new Windows form with zero code use the drop down at the top of the form while viewing form code, select (Formx Events) then in the right drop down select Shown. Once created and recognized you will see the following.

94890-figure1.png



figure1.png (10.2 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi ,I cannot recognize your point of view.Please is it possible a little explain.I don't know what is zero code.
Thanks

0 Votes 0 ·

All you need to do is use the drop downs as in my last reply and Visual Studio will generate the Shown event for you. So what I mean by zero code is you don't code the event the IDE creates it for you.

0 Votes 0 ·

Hi ,it doesn't work.I don't know where I have done wrong.
Thanks

0 Votes 0 ·
Show more comments
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered kavehrahimi-5744 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi ,Thanks my msgbox error is solved now.But the form load error exists yet.
I wrote it so:
Private Sub Form1.shown_Event(sender As Object, e As EventArgs) Handles MyBase.Load
I receive: end of statement expected
Please help me.
Thank you

0 Votes 0 ·

Form Shown should be written as follows

     Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    
     End Sub
0 Votes 0 ·

Hi ,I have yet problem with my code a part of it is:
Private Sub Form1_shown(sender As Object, e As EventArgs) Handles Me.shown
Try
pluggedInQuery = New WqlEventQuery
pluggedInQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"
pluggedInWatcher = New ManagementEventWatcher(pluggedInQuery)
pluggedInWatcher.Start()

         pluggedOutQuery = New WqlEventQuery
         pluggedOutQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3"
         pluggedOutWatcher = New ManagementEventWatcher(pluggedOutQuery)
         pluggedOutWatcher.Start()
     Catch ex As Exception
         MessageBox.Show(ex.Message)
     End Try
 End Sub

I receive: event 'shown' cannot be found
I don't know how I have to write that.
Please help
Thanks

0 Votes 0 ·
kavehrahimi-5744 avatar image
0 Votes"
kavehrahimi-5744 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered XingyuZhao-MSFT converted comment to answer

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



figure1.png (59.9 KiB)
figure3.png (24.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered kavehrahimi-5744 commented

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

  • Wayne

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I couldn't find system.management in my solution explorer where do I have to copy system.management.dll?

0 Votes 0 ·