8 Replies - 67948 Views - Last Post: 11 January 2010 - 09:54 AM Rate Topic: -----

#1 anaprog16  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 24-September 07

How to shut down your computer in C#

Post icon  Posted 24 September 2007 - 08:18 PM

Hi, I'm just starting to learn the C# language. Is there anyone here who knows the syntax for shutting down your computer in C#? It's my assignment actually.





*I edited this post to removal the all caps text. Please don't use all caps, it is considered yelling, and wont get you help any faster :)

PsychoCoder

This post has been edited by PsychoCoder: 24 September 2007 - 09:08 PM


Is This A Good Question/Topic? 1
  • +

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to shut down your computer in C#

Posted 24 September 2007 - 09:06 PM

In C#, you could use System.Management classes and the WMI class Win32_OperatingSystem.

The possible flags for controlling the system
  • 0 = Log off the network.
  • 1 = Shut down the system.
  • 2 = Perform a full reboot of the system.
  • 4 = Force any applications to quit instead of prompting the user to close them.
  • 8 = Shut down the system and, if possible, turn the computer off.

Heres an example of using this to shut down a computer with WMI. First you need a reference to the proper name space

using System.Management;



Then your method, I named mine ShutDownComputer:

public static void ShutDownComputer()
{
		ManagementBaseObject outParameters = null;
		ManagementClass sysOS = new ManagementClass("Win32_OperatingSystem");
		sysOS.Get();
		// enables required security privilege.
		sysOS.Scope.Options.EnablePrivileges = true; 
		// get our in parameters
		ManagementBaseObject inParameters = sysOS.GetMethodParameters("Win32Shutdown");
		// pass the flag of 0 = System Shutdown
		inParameters["Flags"] = "1"; 
		inParameters["Reserved"] = "0";
		foreach (ManagementObject manObj in sysOS.GetInstances())
		{
			outParameters = manObj.InvokeMethod("Win32Shutdown", inParameters, null);
		}
}



Hope this helps :)

This post has been edited by PsychoCoder: 24 September 2007 - 09:09 PM

Was This Post Helpful? 2

#8 GnRSlash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 10

Re: How to shut down your computer in C#

Posted 11 January 2010 - 04:33 AM

Hi PsychoCoder,

I am trying to use your code in C#.NET 2008 Express but I am getting this error:

"The type or namespace name 'ManagementBaseObject' could not be found..."
and this is happening to all code that depends of System.Management

and I am using System.Management;

Did not find any help surfing on the net... could anyone help with this?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 130
  • View blog
  • Posts: 316
  • Joined: 26-November 09

Re: How to shut down your computer in C#

Posted 11 January 2010 - 05:38 AM

View PostGnRSlash, on 11 Jan, 2010 - 03:33 AM, said:

"The type or namespace name 'ManagementBaseObject' could not be found..."


Make sure that your program also contains the correct references. You'll definitely need a reference to System.Management and there might be others too. I just checked a project of mine which uses WMI and I also have a reference to System.Management.Instrumentation but that might not be mandatory.
Was This Post Helpful? 0
  • +
  • -

#10 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: How to shut down your computer in C#

Posted 11 January 2010 - 07:39 AM

Hey,

rightcklick on references in you soloution explorer.. Add Reference and find System.Management..

Click OK and everything s fine^^



Greetz

Ben
Was This Post Helpful? 0
  • +
  • -

#11 GnRSlash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 10

Re: How to shut down your computer in C#

Posted 11 January 2010 - 09:00 AM

yes! it is working now!

I did not understand difference between add reference and use 'using".

I tried 2 methods:

1) System.Diagnostics.Process.Start("shutdown","/s /f /t 060 /c " + (char)34 + "YOUR COMPUTER WILL BE TURNED OFF IN 60 seconds" + (char)34);

2) using System.Management

In the second case, shutdown will not occur if there is an application like a notepad with text inside and not saved!!! but the first case, shutdown will FORCE a shutdown even if an application was not saved.

Is there a way to shutdown using second case and forces windows to close all applications without warning that the document was not saved?

thanks
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: How to shut down your computer in C#

Posted 11 January 2010 - 09:15 AM

The using statement allows you to use a class without using the full class name, like Console.WriteLine instead of System.Console.WriteLine.

There are namespaces that are added to a C# project by default. Having them all included would increase the size of the executable your program generates, I believe. When you add a reference to a C# project you are telling C# that you will be using those namespaces and C# will allow you to use those classes.

Intellisnese can help you determine if you need to add a refernce for a namespace. Say you typed ManagementBaseObject in the editor. If you can access that class using a using a little red square will appear at the end of it. If you hover your mouse over the square you will be asked if you want to add a using statement for the classes in that namespace or use the fully qualified name. If no red square appears you will need to add a reference for the classes in the namespace the classes belong to for your project.
Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: How to shut down your computer in C#

Posted 11 January 2010 - 09:20 AM

View PostGnRSlash, on 11 Jan, 2010 - 11:00 AM, said:

2) using System.Management

In the second case, shutdown will not occur if there is an application like a notepad with text inside and not saved!!! but the first case, shutdown will FORCE a shutdown even if an application was not saved.

Is there a way to shutdown using second case and forces windows to close all applications without warning that the document was not saved?

thanks


Re-read PsychoCoder's post, particularly the paragraph regarding "flags."

Also consider what you may do to the user if you shutdown the computer with open documents.
Was This Post Helpful? 0
  • +
  • -

#14 GnRSlash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 10

Re: How to shut down your computer in C#

Posted 11 January 2010 - 09:54 AM

Quote

Re-read PsychoCoder's post, particularly the paragraph regarding "flags."

Also consider what you may do to the user if you shutdown the computer with open documents.


Thanks man, I forgot the flags!! lol sorry for that. I will try again.

My application is too simple, I just need to warm user after 19:00 that his computer will be turned off in 60 seconds and the user have a chance to cancel the shutdown. If he cancel the shutdown, my application will warm him again after 0,5 hours and repeat this process every time the user cancel shutdown.

I need this because users forgot to turn off the PC :)

well, thank you very much, You all help me!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1