Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

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
Activity
0%
 
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
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
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, Free online technical support
 
webmaster forum
shefeekj  Offline
For Technical Information
Activity
0%
 
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
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
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, Free online technical support
 
webmaster forum
shefeekj  Offline
For Technical Information
Activity
0%
 
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
 
webmaster forum
Activity
100%
 
New Coder
Posts: 16
Topics: 0
WWW
January 28, 2012, 03:37:05 AM

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

Mobile Device Management
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 04, 2012, 05:44:03 PM