Programming and Webmasters forum
HomeSearchRecent PostsLoginRegister Contact Us

Username  
Password
Announcing 14th Weekly Contest - From 25 July To 01 August.

Win every week on this forum.

Chek out How To Win?
 

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Shut Down Remote Computer - C#.net

 
webmaster forum
shefeekj  Offline
For Technical Information
Contest Points: 100
 
New Coder
Posts: 26
Topics: 11
WWW
April 14, 2009, 06:35:38 AM

This code is an example of how to shut down a remote computer in C#.net
Code:
using System.Management;

public class Win32OperatingSystem
{
 public static void Shutdown(string machineName, string username, string password)
 {
  ManagementScope Scope = null;
  ConnectionOptions ConnOptions = null;
  ObjectQuery ObjQuery = null;
  ManagementObjectSearcher ObjSearcher = null;
  try
  {
   ConnOptions = new ConnectionOptions();
   ConnOptions.Impersonation = ImpersonationLevel.Impersonate;
   ConnOptions.EnablePrivileges = true;
   //local machine
   if (machineName.ToUpper() == Environment.MachineName.ToUpper() )
   Scope = new ManagementScope(@”\ROOT\CIMV2″, ConnOptions);
   else
   {
    //remote machine
    ConnOptions.Username = username;
    ConnOptions.Password = password;
    Scope = new ManagementScope(@”\\” + machineName + @”\ROOT\CIMV2″, ConnOptions);
   }
   Scope.Connect();
   ObjQuery = new ObjectQuery(”SELECT * FROM Win32_OperatingSystem”);
   ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery );
   foreach( ManagementObject operatingSystem in ObjSearcher.Get())
   {
    MessageBox.Show(”Caption = ” + operatingSystem.GetPropertyValue(”Caption”));
    MessageBox.Show(”Version = ” + operatingSystem.GetPropertyValue(”Version”));
    ManagementBaseObject outParams = operatingSystem.InvokeMethod (”Shutdown”,null,null);

    }
  }
  catch (Exception ex)
 {
 throw ex;
 }
}
}

//Note:
Call the function like:

static void Main(string[] args)
{
 Win32OperatingSystem.Shutdown(@”Machinename”, @”UserName”, @”Pwd”);
}
« Last Edit: April 14, 2009, 06:42:19 AM by polas »

http://www.shefeekj.com
 
webmaster forum
polas  Offline
*
 
Hacker
Gender: Male
Posts: 1224
Topics: 78
WWW
April 14, 2009, 06:58:10 AM

Thanks for the post, I am a little confused - do you need this running on the remote machine too, or can you just run it from your local machine to work on a remote one? Seems quite an unsecure OS that allows itself to be shutdown remotely.....

Mesham Type Oriented Parallel Programming Language
Skydive in North East England
 
webmaster forum
shefeekj  Offline
For Technical Information
Contest Points: 100
 
New Coder
Posts: 26
Topics: 11
WWW
April 14, 2009, 07:08:18 AM

NO application need to be run in the remote machine....It works fine......yeah OS is  quite unsecure......

http://www.shefeekj.com
 
webmaster forum
polas  Offline
*
 
Hacker
Gender: Male
Posts: 1224
Topics: 78
WWW
April 14, 2009, 07:12:38 AM

Sorry, not entirely clear, I think you are saying it doesn't need to be run on the remote machine... in such case Windows security is worse than I thought!

Mesham Type Oriented Parallel Programming Language
Skydive in North East England
 
webmaster forum
shefeekj  Offline
For Technical Information
Contest Points: 100
 
New Coder
Posts: 26
Topics: 11
WWW
April 14, 2009, 08:21:31 AM

sorry,....... yes u r right.....You can shut down any machine in the network using this code.No code need to be implemented on the remote machine which we are shutting down

http://www.shefeekj.com
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC


Google visited last this page July 29, 2010, 01:16:09 AM

Valid XHTML 1.0 Transitional     Valid XHTML 1.0 Transitional