65.9K
CodeProject 正在变化。 阅读更多。
Home

VB.NET 2003 和 C#.NET 2008 实现远程关机

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.32/5 (32投票s)

2006 年 3 月 23 日

CPOL
viewsIcon

196436

downloadIcon

16662

远程关机、重启和注销您的远程电脑。

Sample screenshot

Sample screenshot

引言

此应用程序可用于关机、重启和注销远程电脑。客户端应用程序 'remoteClientX' 必须安装在客户端电脑上,'ServerX' 用于发送关机、重启和注销等命令。

remoteClientX 的监听状态模块

Sub ListenToServer()
  Try
    Dim LISTENING As Boolean
    Dim localhostAddress As IPAddress = IPAddress.Parse(ipAddress.ToString)
    Dim port As Integer = 63000
    '' PORT ADDRESS
    ''''''''''' making socket tcpList 
    ''''''''''''''''
    Dim tcpList As New TcpListener(localhostAddress, port)
    Try
      tcpList.Start()
      LISTENING = True
      Do While LISTENING
        Do While tcpList.Pending = False And LISTENING = True
          ' Yield the CPU for a while.
          Thread.Sleep(10)
        Loop
        If Not LISTENING Then Exit Do

        Dim tcpCli As TcpClient = tcpList.AcceptTcpClient()
        Dim ns As NetworkStream = tcpCli.GetStream
        Dim sr As New StreamReader(ns)
        ''''''''' get data from server'''''''''''''''
        Dim receivedData As String = sr.ReadLine()
        If receivedData = "###SHUTDOWN###" Then
          trShutdown = New Thread(AddressOf shutdown)trShutdown.Start()
        End If
        If receivedData = "###REBOOT###" Then
          trReboot = New Thread(AddressOf reboot)trReboot.Start()
        End If
        If receivedData = "###LOGOFF###" Then
          trLogOff = New Thread(AddressOf logoff)trLogOff.Start()
        End If
        Dim returnedData As String = "###OK###" '& " From Server"
        Dim sw As New StreamWriter(ns)
        sw.WriteLine(returnedData)
        sw.Flush()
        sr.Close()
        sw.Close()
        ns.Close()
        tcpCli.Close()
      Loop
      tcpList.Stop()
    Catch ex As Exception
      'error
      LISTENING = False
    End Try
End Sub

这里端口地址设置为 63000。

ServerX 关机请求模块

Sub SendMessage()
  Dim host As String = txtClientIP.Text
  Dim port As Integer = 63000
  Try
    Dim tcpCli As New TcpClient(host, port)
    Dim ns As NetworkStream = tcpCli.GetStream

    ' Send data to the client.
    Dim sw As New StreamWriter(ns)
    If rbShutdown.Checked = True Then
        sw.WriteLine("###SHUTDOWN###")
    End If

    If rbReboot.Checked = True Then
        sw.WriteLine("###REBOOT###")
    End If
    If rbLogOff.Checked = True Then
        sw.WriteLine("###LOGOFF###")
    End If
    If rbNothing.Checked = True Then
        sw.WriteLine("to")
    End If
    sw.Flush()
    ' Receive and display data.
    Dim sr As New StreamReader(ns)
    Dim result As String = sr.ReadLine()
    If result = "###OK###" Then
        MsgBox("Operation Performed!!!", MsgBoxStyle.Information, _
               "Accepted by client")
    End If

    sr.Close()
    sw.Close()
    ns.Close()
  Catch ex As Exception
    MsgBox(ex.Message)
  End Try
End Sub

注意remoteClientX 必须在远程电脑上运行,并检查网络 IP 地址。

t = objComputer.Win32Shutdown(8 + 4, 0)

上述语句用于关机。这里 8 是关机的代码,4 是强制执行的代码,即 8+4:强制关机,重启和注销也一样。

Force shutdown: 8+4
Force reboot    : 2+4
LogOff            : 0

注意remoteClientX 使用 WMI,即 Win32_OperatingSystem 类,用于强制关机、强制重启和注销。

© . All rights reserved.