Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Log off, Restart and Shutdown your System Using VB.Net

Author: DevASP
Download Source Code : 354_shutdown_window.zip

In this article you will learn some event of your window like how you can restart your system, log off and also shutdown.

Create a VB.Net application and drag three buttons on the form Now write code against that. First write code against load event of form

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Button1.Text = "Log off"

        Button2.Text = "Restart"

        Button3.Text = "Shutdown"

 

End Sub

  • Write code on button1 click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

Dim t As Single

Dim objWMIService, objComputer As Object

'Now get some privileges

objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")

 

For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")

   t = objComputer.Win32Shutdown(0, 0)

   If t <> 0 Then

       MsgBox("Error occurred!!!")

   Else

      'LogOff your system

   End If

Next

 

End Sub

  •  Write code on button1 click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

 

Dim t As Single

Dim objWMIService, objComputer As Object

'Now get some privileges

objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")

 

For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")

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

   If t <> 0 Then

      MsgBox("Error occurred!!!")

   Else

      'LogOff your system

   End If

Next

End Sub

  • Write code on button3 click event

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

 

Dim t As Single

Dim objWMIService, objComputer As Object

'Now get some privileges

objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")

 

For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")

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

   If t <> 0 Then

      MsgBox("Error occurred!!!")

   Else

      'LogOff your system

   End If

Next

End Sub

Article Comments
that was really wonderful thannks for your help please explain me the logic of this project

Posted on 12/14/2007 2:05:40 AM by Narender singh

every time whilt the system shutsdown.. an error occurs and it won't shutdown until we press end now..

why this error occurs ant wat is the solution for that...

Posted on 10/29/2008 6:03:06 AM by Sandeep

This article is very important for me. Therefore my need now is How to Make a dll containing a form? or How can I create a form by dll and call it?

Posted on 6/2/2010 7:54:52 AM by roddyso

Has anyone had this work with a normal user privileges?

Posted on 8/12/2010 8:32:03 AM by Roger

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How you can make one Click Event of Different Buttons.

Disclaimer - Privacy
© 2002-2017 DevASP.net