使用Visual Studio 來開發程式, 不只在ASPX有很不錯的功能, 再WINFORM方面也是學幾年都學不完.

以下是一個簡單的範例程式, 作用是監控自己的電腦如果建立一個帳號了, 就會顯示一個對話框, 告訴你所建立的帳號名稱及SID.

當然如果你會舉一反三, 也可以寫類似的程式(利用System.Management的namespace)來玩很多的花樣.

WinForm 程式:

Imports System.Management

Public Class Form1

    Dim watcher As ManagementEventWatcher

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Button2.PerformClick()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim equery As New WqlEventQuery("__InstanceCreationEvent", New TimeSpan(0, 0, 2), "TargetInstance isa ""Win32_Account""")
        watcher = New ManagementEventWatcher(equery)
        AddHandler watcher.EventArrived, AddressOf watcher_EventArrived
        watcher.Start()
        Button1.Enabled = False
    End Sub

    Sub watcher_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)

        Dim mbo As ManagementBaseObject
        mbo = CType(e.NewEvent, ManagementBaseObject)

        Dim serviceMO As ManagementBaseObject
        serviceMO = CType(mbo("TargetInstance"), ManagementBaseObject)
        MessageBox.Show(String.Format("Create a New Account: {0}" & vbCrLf & "SID:{1}", serviceMO("Name"), serviceMO("SID")))
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If watcher Is Nothing Then

            Exit Sub

        End If
        watcher.Stop()

        Button1.Enabled = True
        Button2.Enabled = False
    End Sub
End Class

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>