473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reboot machine

Hi,

what is the quick code in C# for reboot local machine?

Thanks.
Jun 3 '06 #1
8 8287
Hello Avi,

Use WMI for this, it's natural way

Sub RebootComputer( strServer )
Set objOSSet = GetObject("winmgmts:{(RemoteShutdown)}//" & strServer
& "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

For each objOS in objOSSet
objOS.Reboot()
Next
End Sub
AG> Hi,
AG>
AG> what is the quick code in C# for reboot local machine?
AG>
AG> Thanks.
AG>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 3 '06 #2
i'm looking for the code in C# for reboot machine...

"Michael Nemtsev" wrote:
Hello Avi,

Use WMI for this, it's natural way

Sub RebootComputer( strServer )
Set objOSSet = GetObject("winmgmts:{(RemoteShutdown)}//" & strServer
& "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

For each objOS in objOSSet
objOS.Reboot()
Next
End Sub
AG> Hi,
AG>
AG> what is the quick code in C# for reboot local machine?
AG>
AG> Thanks.
AG>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Jun 3 '06 #3
Hello Avi,

Did you try to look in MSDN for the info how to use WMI (Management namespace)
and try to convert my script to C#?
It takes less than 5 mins for this (sample in MSDN is available, you need
only point correct query and method)

//Connect to the remote computer
System.Management.ManagementScope ms = new ManagementScope("\\\\<your_server_name>\\root\\cim v2");
ms.Connect();
//Query remote computer across the connection
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT
* FROM Win32_OperatingSystem");

ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,
oq);
ManagementObjectCollection queryCollection1 = query1.Get();

foreach (ManagementObject mo in queryCollection1)
{
string[] ss ={ "" };
mo.InvokeMethod("Reboot", ss);
Console.WriteLine(mo.ToString());
}

AG> i'm looking for the code in C# for reboot machine...
AG>
AG> "Michael Nemtsev" wrote:
AG>
Hello Avi,

Use WMI for this, it's natural way

Sub RebootComputer( strServer )
Set objOSSet = GetObject("winmgmts:{(RemoteShutdown)}//" & strServer
& "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where
Primary=true")
For each objOS in objOSSet
objOS.Reboot()
Next
End Sub
AG> Hi,
AG>
AG> what is the quick code in C# for reboot local machine?
AG>
AG> Thanks.
AG>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents
do not cease to be insipid." (c) Friedrich Nietzsche

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 3 '06 #4
Hi Avi,
what is the quick code in C# for reboot local machine?


Unfortunately, there's no "one-liner" that you could call in C# to reboot
the machine. Instead, it is a little bit more complex due to special
privileges you will need. The Win32 API function to reboot the machine is
ExitWindowsEx. See this page for details:

http://msdn.microsoft.com/library/de...twindowsex.asp

Now, to the C# code. There is the direct P/Invoke route, and the WMI route
that Michael Nemtsev pointer out. Try this page for tips:

http://www.dotnet247.com/247reference/msgs/9/49583.aspx

Good luck!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Jun 3 '06 #5
Hello Jani Järvinen [MVP],

There is the sample of using ExitWindowsEx from C#
http://www.pinvoke.net/default.aspx/...WindowsEx.html
what is the quick code in C# for reboot local machine?

J> Unfortunately, there's no "one-liner" that you could call in C# to
J> reboot the machine. Instead, it is a little bit more complex due to
J> special privileges you will need. The Win32 API function to reboot
J> the machine is ExitWindowsEx. See this page for details:
J>
J> http://msdn.microsoft.com/library/de...ary/en-us/shut
J> down/base/exitwindowsex.asp
J>
J> Now, to the C# code. There is the direct P/Invoke route, and the WMI
J> route that Michael Nemtsev pointer out. Try this page for tips:
J>
J> http://www.dotnet247.com/247reference/msgs/9/49583.aspx

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 3 '06 #6
"Avi G" <Av**@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...

what is the quick code in C# for reboot local machine?


Reminds me of the good old days, when you could write REBOOT.COM (using DOS
DEBUG) in a couple of asm statements...
Jun 3 '06 #7
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:eu**************@TK2MSFTNGP05.phx.gbl...
"Avi G" <Av**@discussions.microsoft.com> wrote in message
news:0A**********************************@microsof t.com...

what is the quick code in C# for reboot local machine?


Reminds me of the good old days, when you could write REBOOT.COM (using
DOS DEBUG) in a couple of asm statements...


:-) I still have a copy of that on an old floppy disk somewhere... :-)
Jun 3 '06 #8

"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fi> wrote in message
news:OK**************@TK2MSFTNGP04.phx.gbl...
| Hi Avi,
|
| > what is the quick code in C# for reboot local machine?
|
| Unfortunately, there's no "one-liner" that you could call in C# to reboot
| the machine. Instead, it is a little bit more complex due to special
| privileges you will need. The Win32 API function to reboot the machine is
| ExitWindowsEx. See this page for details:
|
|
http://msdn.microsoft.com/library/de...twindowsex.asp
|
| Now, to the C# code. There is the direct P/Invoke route, and the WMI route
| that Michael Nemtsev pointer out. Try this page for tips:
|
| http://www.dotnet247.com/247reference/msgs/9/49583.aspx
|
| Good luck!
|

Add to that a simple Process.Start call that issues the shutdown.exe
program.

Willy.
Jun 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4973
by: ---------- Eric ---------- | last post by:
How would i go about rebooting a remote computer from code running on this local computer? Bacsically i have a service running on machine A, it checks the status of a service on machine B. If...
1
1591
by: ------------------ eric -------------------- | last post by:
Any idea how to reboot a givin machine useing the ststem.management namespace? thanks eric.
0
1463
by: Manoj Nair | last post by:
Hi, I am writing a console application using WMI with C# . Development is for Windows XP machine. One of the methods requires to reboot the local terminal. In my application I make a call to...
1
1920
by: Howard Pinsley | last post by:
Every time I reboot my machine, I can no longer run ASP.NET applications. I have to run aspnet_regiis.exe /i after every reboot. The error: Server Application Unavailable The web application...
4
1420
by: martijn | last post by:
Hi, I am trying to create an application for our servicedesk. With this application the servicedesk members, can remotely image an workstation. What am looking for is a command line that...
0
259
by: Loretta | last post by:
I have been trying to install vb.net 2002 and keep getting the following error: Setup has detected that another program requires the computer to reboot. You must reboot the computer before...
12
1871
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when...
3
4001
by: iamsiju | last post by:
Hi, I am facing a problem with the Perl Telnet Object. My Telnet Object hangs just after issueing reboot command on the remote host. Please help me who faced this problem !!! Thanks in...
0
1432
by: remya1000 | last post by:
i'm using VB.NET. i wrote a Auto Reboot program. and here is the codes i tried... Code: Private Enum ShutDown1 LogOff = 0 Shutdown = 1 Reboot = 2 ...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.