Click here to Skip to main content
14,092,036 members
Click here to Skip to main content
Add your own
alternative version

Stats

166.9K views
15.8K downloads
80 bookmarked
Posted 23 Mar 2006
Licenced CPOL

Remote Shutdown in VB.NET 2003 and C#.NET 2008

Rate this:
Please Sign up or sign in to vote.
Shutdown, reboot, and log-off your remote PCs.
Sample screenshot

Sample screenshot

Introduction

This application can be used to shutdown, reboot, and logoff remote PCs. Client application that is 'remoteClientX' must be installed on client PCs and 'ServerX' is used to give commands like shutdown, reboot and logoff.

Listen State Module of 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

Here port address is taken as 63000.

ServerX Shutdown Request Module

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

Note: remoteClientX must be run on remote PCs & check the network IP address.

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

The above statement is used to shutdown the system. Here 8 is the code for shutdown and 4 is for Force that is 8+4 : force shutdown, same for reboot and logoff.

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

Note: remoteClientX uses WMI that is Win32_OperatingSystem class for force shutdown, force reboot, logoff.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hari Om Prakash Sharma
Software Developer
India India
Developer

You may also be interested in...

Comments and Discussions

 
GeneralThnx Hari! Pin
tw1st3d9-May-07 19:18
membertw1st3d9-May-07 19:18 
GeneralRe: Thnx Hari! Pin
Hari Om Prakash Sharma10-May-07 23:21
memberHari Om Prakash Sharma10-May-07 23:21 
GeneralShutdown command Pin
King_kLAx11-Sep-06 0:27
memberKing_kLAx11-Sep-06 0:27 
GeneralRe: Shutdown command Pin
Hari Om Prakash Sharma11-Sep-06 3:43
memberHari Om Prakash Sharma11-Sep-06 3:43 
GeneralRe: Shutdown command Pin
King_kLAx15-Sep-06 0:17
memberKing_kLAx15-Sep-06 0:17 
Generalsome guidance on my project Pin
manish30127-Aug-06 22:34
membermanish30127-Aug-06 22:34 
GeneralRe: some guidance on my project Pin
baljeet_1237-Nov-07 2:53
memberbaljeet_1237-Nov-07 2:53 
GeneralAwesome code! Pin
philipg3630-Jul-06 17:52
memberphilipg3630-Jul-06 17:52 
Thanks! I've been trying to look for codes like this type of purpose and up to now I was still kind of hoping to find the same in vb6! But anyway thanks again so much.. really appreciate your effort of helping other developers.Big Grin | :-D
JokeThank's Hari Om Prakash Sharma! Pin
nguyenkiem25-Apr-06 15:40
membernguyenkiem25-Apr-06 15:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190518.1 | Last Updated 3 Jun 2010
Article Copyright 2006 by Hari Om Prakash Sharma
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid