I've written an application for work in .net to shut down computers and start them up again using Wake On Lan. The shutdown portion uses Windows Management Instrumentation. It accesses this through the namespace System.Management. I was wondering if someone could look at my code as my program seems to slow down for about 10-15 seconds after executing the event that shutsdown the computer. It's even worse in a loop I setup that will shutdown all the comptuers at a certain time using a timer event.
Here is the shutdown class that I made.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShutdownComputers
{
class Shutdown
{
public void Win32Shutdown(string compName, string userName, string password, string domain)
{
System.Management.ConnectionOptions connection = new System.Management.ConnectionOptions();
connection.EnablePrivileges = true;
connection.Authority = "NTLMDomain:" + domain;
connection.Username = userName;
connection.Password = password;
System.Management.ManagementScope scope =
new System.Management.ManagementScope(@"\\" + compName + @"\root\cimv2", connection);
scope.Options.EnablePrivileges = true;
scope.Connect();
System.Management.ObjectQuery query =
new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
System.Management.ManagementObjectSearcher objSearcher =
new System.Management.ManagementObjectSearcher(scope, query);
System.Management.ManagementObjectCollection objCol = objSearcher.Get();
foreach (System.Management.ManagementObject mo in objCol)
{
string[] methodArgs = { "5", "0" }; // {0}, {0} log off
// {4}, {0} forced log off
// {1}, {0} shutdown
// {5}, {0} forced shutdown
// {2}, {0} reboot
// {6}, {0} forced reboot
// {8}, {0} power off
// {C}, {0} forced power off
mo.InvokeMethod("Win32Shutdown", methodArgs);
}
}
}
}
Here are the events that use it.
private void btnShutdown_Click(object sender, EventArgs e)
{
lstStatus.Items.Clear();
try
{
Shutdown shutdown = new Shutdown();
shutdown.Win32Shutdown(lstDBComputerName.SelectedValue.ToString(), txtUsername.Text, txtPassword.Text, "public");
}
catch
{
lstStatus.Items.Add("Enter Username/Password");
}
}
private void OnTick(object sender, EventArgs e)
{
if (// Sunday StartUp
dsPCInfo.DateTime.Rows[0]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[0]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[0]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[0]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[0]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Monday StartUp
|| dsPCInfo.DateTime.Rows[1]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[1]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[1]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[1]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[1]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Tuesday StartUp
|| dsPCInfo.DateTime.Rows[2]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[2]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[2]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[2]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[2]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Wednesday StartUp
|| dsPCInfo.DateTime.Rows[3]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[3]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[3]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[3]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[3]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Thursday StartUp
|| dsPCInfo.DateTime.Rows[4]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[4]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[4]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[4]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[4]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
//Friday StartUp
|| dsPCInfo.DateTime.Rows[5]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[5]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[5]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[5]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[5]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Saturday StartUp
|| dsPCInfo.DateTime.Rows[6]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[6]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[6]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[6]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[6]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString())
{
WakeOnLan wakeUp = new WakeOnLan();
lstStatus.Items.Clear();
for (int j = 0; j < dsPCInfo.Machines.Rows.Count; j++)
{
try
{
string strMac = dsPCInfo.Machines.Rows[j]["MACAddress"].ToString();
string strMac1 = strMac.Substring(0, 2);
string strMac2 = strMac.Substring(3, 2);
string strMac3 = strMac.Substring(6, 2);
string strMac4 = strMac.Substring(9, 2);
string strMac5 = strMac.Substring(12, 2);
string strMac6 = strMac.Substring(15, 2);
byte mac1 = Byte.Parse(strMac1, NumberStyles.HexNumber);
byte mac2 = Byte.Parse(strMac2, NumberStyles.HexNumber);
byte mac3 = Byte.Parse(strMac3, NumberStyles.HexNumber);
byte mac4 = Byte.Parse(strMac4, NumberStyles.HexNumber);
byte mac5 = Byte.Parse(strMac5, NumberStyles.HexNumber);
byte mac6 = Byte.Parse(strMac6, NumberStyles.HexNumber);
lstStatus.Items.Add(dsPCInfo.Machines.Rows[j]["ComputerName"].ToString()
+ " is starting up");
byte[] mac = new byte[] { mac1, mac2, mac3, mac4, mac5, mac6 };
wakeUp.WakeUp(mac);
}
catch
{
continue;
}
}
}
if (// Sunday Shutdown
dsPCInfo.DateTime.Rows[7]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[7]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[7]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[7]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[7]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Monday Shutdown
|| dsPCInfo.DateTime.Rows[8]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[8]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[8]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[8]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[8]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Tuesday Shutdown
|| dsPCInfo.DateTime.Rows[9]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[9]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[9]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[9]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[9]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Wednesday Shutdown
|| dsPCInfo.DateTime.Rows[10]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[10]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[10]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[10]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[10]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Thursday Shutdown
|| dsPCInfo.DateTime.Rows[11]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[11]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[11]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[11]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[11]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
//Friday Shutdown
|| dsPCInfo.DateTime.Rows[12]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[12]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[12]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[12]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[12]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString()
// Saturday Shutdown
|| dsPCInfo.DateTime.Rows[13]["DayOfWeek"].ToString()
== DateTime.Now.DayOfWeek.ToString()
&& dsPCInfo.DateTime.Rows[13]["Hour"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[13]["Minute"].ToString()
+ ":" + dsPCInfo.DateTime.Rows[13]["Second"].ToString()
+ " " + dsPCInfo.DateTime.Rows[13]["AMPM"].ToString()
== DateTime.Now.ToLongTimeString())
{
//ShutdownPCs shutdownPCs = new ShutdownPCs();
Shutdown shutdown = new Shutdown();
lstStatus.Items.Clear();
for (int j = 0; j < dsPCInfo.Machines.Rows.Count; j++)
{
try
{
/*if (PingPC.Start(dsPCInfo.Machines.Rows[j]["ComputerName"].ToString()) == true)
{*/
lstStatus.Items.Add(dsPCInfo.Machines.Rows[j]["ComputerName"].ToString()
+ " is shuting down");
//shutdownPCs.Shutdown(dsPCInfo.Machines.Rows[j]["ComputerName"].ToString(), null, null);
shutdown.Win32Shutdown(dsPCInfo.Machines.Rows[j]["ComputerName"].ToString(), txtUsername.Text, txtPassword.Text, "public");
/*}*/
}
catch
{
continue;
}
}
}
}
Thanks