How to Shutdown My Computer using C#?

427 views
Skip to first unread message

Khaled Atta

unread,
Apr 1, 2004, 12:31:18 AM4/1/04
to
Hi Every Body
I wanted to know how can I use the WMI to shutdown my Computer in C#
Thank you very much :)

Jiachuan Wang [MSFT]

unread,
Apr 5, 2004, 1:30:39 PM4/5/04
to
This can be used to shutdown locally or remotely by changing the following:
(@"machineName", @"domain\user", @"password")

using System;

using System.Management;

namespace Community_CS

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

Win32OperatingSystem.Shutdown(@"machineName", @"domain\user", @"password");

Console.ReadLine();

}

}

public class Win32OperatingSystem

{

public static void Shutdown(string machineName, string username, string
password)

{

ManagementScope sc = null;

ConnectionOptions co = null;

ObjectQuery oq = null;

ManagementObjectSearcher os = null;

try

{

co = new ConnectionOptions();

co.Impersonation = ImpersonationLevel.Impersonate;

co.EnablePrivileges = true;

if (machineName.ToUpper() == Environment.MachineName.ToUpper() )

sc = new ManagementScope(@"\ROOT\CIMV2", co);

else

{

co.Username = username;

co.Password = password;

sc = new ManagementScope(@"\\" + machineName + @"\ROOT\CIMV2", co);

}

sc.Connect();

oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");

os = new ManagementObjectSearcher(sc, oq);

foreach( ManagementObject operatingSystem in os.Get())

{

Console.WriteLine("Caption = " +
operatingSystem.GetPropertyValue("Caption"));

Console.WriteLine("Version = " +
operatingSystem.GetPropertyValue("Version"));

ManagementBaseObject outParams = operatingSystem.InvokeMethod ("Shutdown",
null, null);

Console.WriteLine("Shutdown returned: " + outParams["returnValue"]);

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

os.Dispose();

}

}

}

}


--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Khaled Atta" <kha...@netvis.co.uk> wrote in message
news:unq$Pq6FEH...@TK2MSFTNGP11.phx.gbl...

Reply all
Reply to author
Forward
0 new messages