Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
4.32/5 (37 votes)
3 Jun 2010CPOL 190.2K   16.6K   81   43
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

VB.NET
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

VB.NET
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.

C#
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)


Written By
Software Developer
India India
Developer

Comments and Discussions

 
BugI tried got error Pin
Member 1474681120-Feb-20 0:50
Member 1474681120-Feb-20 0:50 
QuestionDo you change any permission in Firewall for this program? Pin
Umut Comlekcioglu24-Jul-15 5:49
professionalUmut Comlekcioglu24-Jul-15 5:49 
AnswerRe: Do you change any permission in Firewall for this program? Pin
Hari Om Prakash Sharma26-Jul-15 6:55
Hari Om Prakash Sharma26-Jul-15 6:55 
Questionadd various ip Pin
naq_ninetwo9218-Oct-12 5:20
naq_ninetwo9218-Oct-12 5:20 
AnswerRe: add various ip Pin
Hari Om Prakash Sharma5-Dec-12 17:29
Hari Om Prakash Sharma5-Dec-12 17:29 
GeneralRe: add various ip Pin
naq_ninetwo924-Feb-13 14:34
naq_ninetwo924-Feb-13 14:34 
GeneralMy vote of 1 Pin
Аslam Iqbal10-Aug-12 17:46
professionalАslam Iqbal10-Aug-12 17:46 
GeneralMy vote of 3 Pin
Vitaly Tomilov23-Jun-12 23:17
Vitaly Tomilov23-Jun-12 23:17 
GeneralArticle Viewer in C#.Net Pin
Mohammad Afrashteh9-Feb-11 12:36
Mohammad Afrashteh9-Feb-11 12:36 
GeneralHelp needed Pin
servox4-Aug-10 12:13
servox4-Aug-10 12:13 
AnswerRe: Help needed Pin
Hari Om Prakash Sharma10-Aug-10 23:43
Hari Om Prakash Sharma10-Aug-10 23:43 
GeneralSysinternal did this a long time ago.... Pin
giammin3-Jun-10 21:49
giammin3-Jun-10 21:49 
GeneralRe: Sysinternal did this a long time ago.... Pin
Hari Om Prakash Sharma4-Jun-10 5:37
Hari Om Prakash Sharma4-Jun-10 5:37 
Generalremote shutdown error Pin
alanz857-Apr-10 23:16
alanz857-Apr-10 23:16 
GeneralRe: remote shutdown error Pin
Hari Om Prakash Sharma11-Apr-10 3:21
Hari Om Prakash Sharma11-Apr-10 3:21 
Generalcode Pin
happyankitsanghvi1-Jan-10 22:28
happyankitsanghvi1-Jan-10 22:28 
AnswerRe: code Pin
Hari Om Prakash Sharma3-Jan-10 23:54
Hari Om Prakash Sharma3-Jan-10 23:54 
Generalplease Pin
Member 40574751-Oct-09 8:28
Member 40574751-Oct-09 8:28 
QuestionRemote start up server Pin
kuniyo7-Jul-09 23:11
kuniyo7-Jul-09 23:11 
AnswerRe: Remote start up server Pin
Hari Om Prakash Sharma26-Jul-09 19:30
Hari Om Prakash Sharma26-Jul-09 19:30 
GeneralShutdown 1000 XP SP2 PCs Pin
neoice12-Dec-08 22:39
neoice12-Dec-08 22:39 
GeneralRe: Shutdown 1000 XP SP2 PCs Pin
Hari Om Prakash Sharma26-Jul-09 19:36
Hari Om Prakash Sharma26-Jul-09 19:36 
QuestionRemote data on com port Pin
fahadi8610-Dec-08 10:20
fahadi8610-Dec-08 10:20 
AnswerRe: Remote data on com port Pin
Hari Om Prakash Sharma17-Dec-08 22:57
Hari Om Prakash Sharma17-Dec-08 22:57 
Generalmayank Pin
rajnik2-Dec-08 1:11
rajnik2-Dec-08 1:11 

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.