C#.net

Shut Down Remote Computer – C#.net



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: 
[STAThread] 
static void Main(string[] args) 
{ 
Win32OperatingSystem.Shutdown(@”Machinename”, @”UserName”, @”Pwd”); 
}

Share

Thanks for reading my blog. If you like what I write, why not subscribe to my feed?

If you are busy, I can send the latest post to your email. Just subscribe to my email updates.

 

Enter your email address:

Delivered by FeedBurner

Discussion

3 comments for “Shut Down Remote Computer – C#.net”

  1. hey janith here.. what is this code for? why do u say remote machine?

    Posted by janith | February 2, 2009, 11:23 am
  2. thank you…

    Posted by rishi | February 5, 2010, 10:50 pm

Post a comment