Module Module1 Private _ComputerName As String = "." Private _UserName As String = "Changeme to Runon a remote machine" Private _Password As String = "Changeme to Runon a remote machine" Private _SelectQuery As String = "SELECT * FROMWin32_Process WHERE name='w3wp.exe'" Private _DateEntered As DateTime =System.DateTime.Now()
Sub Main() Try GetProcessInfo() Catch f As Exception ErrorHandler(f) End Try End Sub
Sub GetProcessInfo() Try '********************************************** 'Uncomment this out if you want to run on a remotemachine and 'comment out the other 'scope' variable 'You will get an error about trying passcredentials to the local machine ' 'Dim options As New ConnectionOptions() 'options.Username = _UserName 'options.Password = _Password
'Dim scope As New ManagementScope("\\"& _ComputerName & "\root\cimv2", options) '**********************************************
'Comment this out if you are going to test on aremote machine 'This will default to authenticating to the localmachine Dim scope As New ManagementScope("\\"& _ComputerName & "\root\cimv2")
Dim selectQuery As New SelectQuery(_SelectQuery) Dim searcher As NewManagementObjectSearcher(scope, selectQuery)
Try scope.Connect() Catch f As Exception ErrorHandler(f) Exit Sub End Try
For Each queryObj As ManagementObject Insearcher.Get() Console.WriteLine(queryObj("WorkingSetSize") / 1024) Console.WriteLine(queryObj("PeakWorkingSetSize") / 1024) Console.WriteLine(queryObj("PageFileUsage")/ 1024) Console.WriteLine(queryObj("PeakPageFileSystem") / 1024) Console.WriteLine(GetOwnerObject(queryObj("Handle"))) Next Catch f As Exception ErrorHandler(f) End Try End Sub
Function GetOwnerObject(ByVal strHandle As String)As Object Dim options As New ConnectionOptions() options.Username = _UserName options.Password = _Password
'********************************************** 'Uncomment this out if you want to run on a remotemachine and 'comment out the other 'scope' variable 'You will get an error about trying passcredentials to the local machine ' 'Dim options As New ConnectionOptions() 'options.Username = _UserName 'options.Password = _Password 'Dim s As New ManagementScope("\\" &_ComputerName & "\root\cimv2", options) '**********************************************
'Comment this out if you are going to test on aremote machine 'This will default to authenticating to the localmachine Dim s As New ManagementScope("\\" &_ComputerName & "\root\cimv2", options)
Dim p As New ManagementPath( _ "Win32_Process.Handle='" & strHandle & "'")
Dim opt As ObjectGetOptions opt = New ObjectGetOptions()
Dim o As ManagementObject o = New ManagementObject(s, p, opt)
' Execute the method and obtain the return values. Dim outParams As ManagementBaseObject = _ o.InvokeMethod("GetOwner", Nothing, Nothing) Return outParams("User").ToString() End Function
Sub ErrorHandler(ByVal Exp As Exception) Dim LogName As String Dim Message As String Dim Log As New System.Diagnostics.EventLog
Imports System.Management
Imports System.Data.SqlClient
Module Module1
Private _ComputerName As String = "."
Private _UserName As String = "Changeme to Runon a remote machine"
Private _Password As String = "Changeme to Runon a remote machine"
Private _SelectQuery As String = "SELECT * FROMWin32_Process WHERE name='w3wp.exe'"
Private _DateEntered As DateTime =System.DateTime.Now()
Sub Main()
Try
GetProcessInfo()
Catch f As Exception
ErrorHandler(f)
End Try
End Sub
Sub GetProcessInfo()
Try
'**********************************************
'Uncomment this out if you want to run on a remotemachine and
'comment out the other 'scope' variable
'You will get an error about trying passcredentials to the local machine
'
'Dim options As New ConnectionOptions()
'options.Username = _UserName
'options.Password = _Password
'Dim scope As New ManagementScope("\\"& _ComputerName & "\root\cimv2", options)
'**********************************************
'Comment this out if you are going to test on aremote machine
'This will default to authenticating to the localmachine
Dim scope As New ManagementScope("\\"& _ComputerName & "\root\cimv2")
Dim selectQuery As New SelectQuery(_SelectQuery)
Dim searcher As NewManagementObjectSearcher(scope, selectQuery)
Try
scope.Connect()
Catch f As Exception
ErrorHandler(f)
Exit Sub
End Try
For Each queryObj As ManagementObject Insearcher.Get()
Console.WriteLine(queryObj("WorkingSetSize") / 1024)
Console.WriteLine(queryObj("PeakWorkingSetSize") / 1024)
Console.WriteLine(queryObj("PageFileUsage")/ 1024)
Console.WriteLine(queryObj("PeakPageFileSystem") / 1024)
Console.WriteLine(GetOwnerObject(queryObj("Handle")))
Next
Catch f As Exception
ErrorHandler(f)
End Try
End Sub
Function GetOwnerObject(ByVal strHandle As String)As Object
Dim options As New ConnectionOptions()
options.Username = _UserName
options.Password = _Password
'**********************************************
'Uncomment this out if you want to run on a remotemachine and
'comment out the other 'scope' variable
'You will get an error about trying passcredentials to the local machine
'
'Dim options As New ConnectionOptions()
'options.Username = _UserName
'options.Password = _Password
'Dim s As New ManagementScope("\\" &_ComputerName & "\root\cimv2", options)
'**********************************************
'Comment this out if you are going to test on aremote machine
'This will default to authenticating to the localmachine
Dim s As New ManagementScope("\\" &_ComputerName & "\root\cimv2", options)
Dim p As New ManagementPath( _
"Win32_Process.Handle='" & strHandle & "'")
Dim opt As ObjectGetOptions
opt = New ObjectGetOptions()
Dim o As ManagementObject
o = New ManagementObject(s, p, opt)
' Execute the method and obtain the return values.
Dim outParams As ManagementBaseObject = _
o.InvokeMethod("GetOwner", Nothing, Nothing)
Return outParams("User").ToString()
End Function
Sub ErrorHandler(ByVal Exp As Exception)
Dim LogName As String
Dim Message As String
Dim Log As New System.Diagnostics.EventLog
Try
LogName = "Application"
Message = "Message: " &Exp.ToString()
If (Not System.Diagnostics.EventLog.SourceExists(LogName))Then
System.Diagnostics.EventLog.CreateEventSource(LogName, LogName)
End If
Log.Source = LogName
Log.WriteEntry(Message)
Log.Close()
Log.Dispose()
Catch ex As Exception
End Try
End Sub
End Module详细信息你可以看这里:http://aspalliance.com/806_CodeSnip_Get_memory_size_usage_using_WMI_and_NET_20