Click here to Skip to main content
Licence CPOL
First Posted 23 Mar 2006
Views 97,798
Downloads 6,293
Bookmarked 73 times

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

By | 3 Jun 2010 | Article
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)

About the Author

Hari Om Prakash Sharma


ISRO
India India

Member

Follow on Twitter Follow on Twitter
Scientist

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralArticle Viewer in C#.Net PinmemberMohammad Afrashteh12:36 9 Feb '11  
GeneralHelp needed Pinmemberservox12:13 4 Aug '10  
AnswerRe: Help needed PinmemberHari Om Prakash Sharma23:43 10 Aug '10  
GeneralSysinternal did this a long time ago.... Pinmembergiammin21:49 3 Jun '10  
GeneralRe: Sysinternal did this a long time ago.... PinmemberHari Om Prakash Sharma5:37 4 Jun '10  
Generalremote shutdown error Pinmemberalanz8523:16 7 Apr '10  
GeneralRe: remote shutdown error PinmemberHari Om Prakash Sharma3:21 11 Apr '10  
Generalcode Pinmemberhappyankitsanghvi22:28 1 Jan '10  
AnswerRe: code PinmemberHari Om Prakash Sharma23:54 3 Jan '10  
Generalplease PinmemberMember 40574758:28 1 Oct '09  
QuestionRemote start up server Pinmemberkuniyo23:11 7 Jul '09  
AnswerRe: Remote start up server PinmemberHari Om Prakash Sharma19:30 26 Jul '09  
GeneralShutdown 1000 XP SP2 PCs Pinmemberneoice22:39 12 Dec '08  
GeneralRe: Shutdown 1000 XP SP2 PCs PinmemberHari Om Prakash Sharma19:36 26 Jul '09  
QuestionRemote data on com port Pinmemberfahadi8610:20 10 Dec '08  
AnswerRe: Remote data on com port PinmemberHari Om Prakash Sharma22:57 17 Dec '08  
Generalmayank Pinmemberrajnik1:11 2 Dec '08  
hi hari om ,thank's for this code Laugh | :laugh:
GeneralRe: mayank PinmemberHari Om Prakash Sharma18:48 15 Dec '08  
GeneralHI Hari Om PinmemberchandrikaC6:07 31 Aug '08  
AnswerRe: HI Hari Om PinmemberHari Om Prakash Sharma18:56 31 Aug '08  
GeneralThank You Hari! Pinmembergorpor18:52 25 May '08  
GeneralRe: Thank You Hari! PinmemberHari Om Prakash Sharma20:44 27 May '08  
Generalcode Pinmemberchanda sharma6:02 30 Mar '08  
AnswerRe: code PinmemberHari Om Prakash Sharma18:16 30 Mar '08  
Generalhai Pinmembersujithomas8:09 9 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120604.1 | Last Updated 3 Jun 2010
Article Copyright 2006 by Hari Om Prakash Sharma
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid