Monday, January 28, 2008

How can I access the System Time on a Remote PC using VB.NET?

Hey, Programming Guy! How can I access the system time on a remote computer using VB.Net?
- Sweena
Hi, Sweena, there are two different techniques for accessing the resources of a remote computer using Visual Basic Dot Net, namely: WMI Scripting and Dot Net's Management package. The first example demonstrate how to retrieve the system time of a remote computer using WMI Scripting Object. The second example uses the Dot Net's Management package for accessing the system time of a remote computer:

Imports System.Net.NetworkInformation
Imports WbemScripting

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(getSystemTime("192.168.1.1", "Administrator", "password"))
End Sub

Private Function getSystemTime(ByVal TargetIP As String, ByVal LoginName As String, ByVal Password As String) As String
Dim oPing As Ping
oPing = New Ping
getSystemTime = ""
If oPing.Send(TargetIP).Status = IPStatus.Success Then
Dim objWbemLocator As SWbemLocator
Dim objWMIservice As SWbemServices
Dim objWMIobjectSet As SWbemObjectSet
Dim objColumn As SWbemObject
Dim strTime As String
objWbemLocator = New SWbemLocator
objWMIservice = objWbemLocator.ConnectServer(TargetIP, "root\cimv2", LoginName, Password)
objWMIobjectSet = objWMIservice.ExecQuery("Select * from Win32_OperatingSystem")
strTime = ""
For Each objColumn In objWMIobjectSet
strTime = objColumn.Properties_.Item("LocalDateTime").Value
Next
getSystemTime = strTime.Substring(8, 2) & ":" & strTime.Substring(10, 2) & ":" & strTime.Substring(12, 2)
End If
End Function
End Class


Imports System.Net.NetworkInformation
Imports System.Management

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(getSystemTime("192.168.1.1", "Administrator", "password"))
End Sub

Private Function getSystemTime(ByVal TargetIP As String, ByVal LoginName As String, ByVal Password As String) As String
Dim oPing As Ping
Dim strTime
oPing = New Ping
getSystemTime = ""
strTime = ""
If oPing.Send(TargetIP).Status = IPStatus.Success Then
Dim connOpts As ConnectionOptions
Dim scope As ManagementScope
Dim query As ManagementObjectSearcher
Dim objColumn As ManagementBaseObject
connOpts = New ConnectionOptions
connOpts.Username = LoginName
connOpts.Password = Password
scope = New ManagementScope("\\" & TargetIP & "\root\cimv2", connOpts)
scope.Connect()
query = New ManagementObjectSearcher("Select * from Win32_OperatingSystem")
query.Scope = scope
For Each objColumn In query.get
strTime = objColumn.GetPropertyValue("LocalDateTime").ToString
Next
getSystemTime = strTime.Substring(8, 2) & ":" & strTime.Substring(10, 2) & ":" & strTime.Substring(12, 2)
End If
End Function
End Class

1 comment:

Terry Hardy said...

I just rely on remote access software for these needs. I just got sick of the limitations of the other connection methods and went with a fully featured and flexible solution.