搜索一下以前的贴子吧,很多都是C#写的,以下是vb.net: Private Sub closepc(ByVal host As String, ByVal username As String, ByVal pws As String, ByVal op As Integer) Dim co As ConnectionOptions = New ConnectionOptions co.Username = username co.Password = pws Dim server As String = host.ToLower '测试对方是否连通 Dim isopen As String Try Dim objPingHost As New pinghost objPingHost.TimeOut = 5000 ' --> 5000 msec Time Out objPingHost.DataSize = 32 ' --> 32 bytes Package Size objPingHost.Host = server If objPingHost.Ping() = objPingHost.Options.SUCCESS Then isopen = "yes" Else isopen = Nothing End If Catch ex As Exception isopen = Nothing End Try 'MsgBox(server & Chr(10) & username & Chr(10) & pws) If Not isopen Is Nothing Then If server = System.Net.Dns.GetHostName Then Exit Sub Try Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" + server + "\root\cimv2", co) Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem") Dim query As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq) Dim queryCollection As ManagementObjectCollection = query.Get() For Each mo As ManagementObject In queryCollection Dim ss As String() = {"", ""} Select Case op Case 1 '注销 ss(0) = "4" ss(1) = "0" Case 2 '关机 ss(0) = "12" ss(1) = "0" Case 3 '重启 ss(0) = "6" ss(1) = "0" End Select mo.InvokeMethod("win32ShutDown", ss) Application.DoEvents() Next Catch ex As Exception MsgBox(server & Chr(10) & ex.Message) End Try End If End Sub
说的我也知道,可是我想知道得更详细一点
Dim co As ConnectionOptions = New ConnectionOptions
co.Username = username
co.Password = pws
Dim server As String = host.ToLower
'测试对方是否连通
Dim isopen As String
Try
Dim objPingHost As New pinghost
objPingHost.TimeOut = 5000 ' --> 5000 msec Time Out
objPingHost.DataSize = 32 ' --> 32 bytes Package Size
objPingHost.Host = server If objPingHost.Ping() = objPingHost.Options.SUCCESS Then
isopen = "yes"
Else
isopen = Nothing
End If
Catch ex As Exception
isopen = Nothing
End Try
'MsgBox(server & Chr(10) & username & Chr(10) & pws)
If Not isopen Is Nothing Then
If server = System.Net.Dns.GetHostName Then Exit Sub
Try
Dim ms As System.Management.ManagementScope = New System.Management.ManagementScope("\\" + server + "\root\cimv2", co)
Dim oq As System.Management.ObjectQuery = New System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim query As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
Dim queryCollection As ManagementObjectCollection = query.Get()
For Each mo As ManagementObject In queryCollection
Dim ss As String() = {"", ""}
Select Case op
Case 1 '注销
ss(0) = "4"
ss(1) = "0"
Case 2 '关机
ss(0) = "12"
ss(1) = "0"
Case 3 '重启
ss(0) = "6"
ss(1) = "0"
End Select
mo.InvokeMethod("win32ShutDown", ss)
Application.DoEvents()
Next
Catch ex As Exception
MsgBox(server & Chr(10) & ex.Message)
End Try
End If
End Sub
{
UserANDPass temp = new UserANDPass();
if(temp.ShowDialog(this) != DialogResult.OK)
{
return;
}
try
{
if(temp.textBoxIP.Text != "")
{ ConnectionOptions co = new ConnectionOptions();
co.Username = temp.textBoxUser.Text;
co.Password = temp.textBoxPass.Text; string tempPath = "\\\\"+temp.textBoxIP.Text+"\\"+temp.comboBoxPath.Text; System.Management.ManagementScope ms = new ManagementScope(tempPath,co); System.Management.ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq); foreach (ManagementObject mo in query1.Get())
{
string[] ss ={""};
mo.InvokeMethod("Reboot",ss);
}
}
else
{
System.Management.ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher query1 = new ManagementObjectSearcher(oq); foreach (ManagementObject mo in query1.Get())
{
string[] ss ={""};
mo.InvokeMethod("Reboot",ss);
this.richTextBoxShell.Text += temp.textBoxIP.Text + "正在重启!";
}
}
}
catch(Exception E)
{
this.richTextBoxShell.Text += "\n";
this.richTextBoxShell.Text += E.Message;
}
}