473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Rebooting

A
Anyone have any code to reboot from a C# app?
Nov 15 '05 #1
4 2139
As far as I know, there is no managed .NET support for this.
However, you can use the "ExitWindowsEx" or "InitiateShutdown" or
"InitiateShutdownEx" Windows API calls.
The first one should be used if initiated from an interactive application,
the last two should be used from
a server application,

Bennie Haelen

"A" <NO****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any code to reboot from a C# app?

Nov 15 '05 #2

Hi,

Thank you for posting in the community!

Based on my understanding, you want to use C# to reboot your computer.
===========================================
Based on my experience, .Net class library does not support the function of
rebooting. You need to get this done through P/invoke some WIN32 APIs.

Bennie has provided you the functions you may use, please look the detailed
remarks in MSDN "PlatForm SDK"

===========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3
You can use the System.Management interfaces to reboot a machine. This uses
WMI (I think it stands for Windows Management Instrumentation ). This will
also allow you to reboot remote machines too if you wanted to.

Here's some code, may need some changes, I just copy/pasted from my code. If
the current user has permissions to reboot the machine, you can comment out
the connection_options stuff also.

using System.Management;

/// ----------------- Need class dedfinition...

int result;

result = 0;

string host;

host = "LOCALHOST";

ConnectionOptions Connect_options = new ConnectionOptions();

Connect_options.Username = user; // enter username

Connect_options.Password = password; // enter password

ManagementScope scope = new
ManagementScope("\\\\"+host+"\\root\\cimv2",Connec t_options);

scope.Options.EnablePrivileges = true;

scope.Connect();

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

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

foreach( ManagementObject mo in queryCollection1 )

{

string[] ss={"6","0"};

mo.InvokeMethod("Win32Shutdown",ss);

Console.WriteLine(mo.ToString());

}

"A" <NO****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Anyone have any code to reboot from a C# app?

Nov 15 '05 #4

Hi

Is your problem resolved?
If you still have anything unclear, please feel free to feedback to me. I
will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

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

Similar topics

0
by: Al-ahmadi | last post by:
Hi EveryBody: Is there any way to add the rebooting code to the user interface of the setup project in VB.net ? if the answer is yes,how can I do it ? but if the answer is no, Is there any way...
0
by: Todd Bright | last post by:
I have a Windows service that I wrote in C#. It uses a form in another thread to keep it running because we needed some user interface activity. When the service is running the machine will NOT...
2
by: mr_amitkulkarni | last post by:
Hello Friends, Objective : I'm working on a trivial solution for an issue like if my server/pc hangs at any moment it should reboot automatically. proposed Solution : I'll be using an external...
2
by: Brady Kelly | last post by:
When I use the ExitWindowsEx function in my C# console application it doesn't reboot. Any ideas why?
8
by: Steve Teeples | last post by:
I am trying to use WMI to reboot my system. I have admin privileges. I've tried two different solutions but both fail for separate reasons. private void button1_Click(object sender,...
1
by: Josh | last post by:
The error I am getting is the following. ALong with the error asp doesnt start and the web site is down until I reregister asp service. Event Type: Error Event Source: ASP.NET...
1
by: Al-ahmadi | last post by:
Hi EveryBody: Is there any way to add the rebooting code to the user interface of the setup project in VB.net ? if the answer is yes,how can I do it ? but if the answer is no, Is there any way...
7
by: Raju5725 | last post by:
Hi All, How can I Change Local Machine IP Address without rebooting the machine using VB.Net. As I want to shift from one IP address to another IP address with application the effect should...
3
by: spoonone | last post by:
Hi my daughters PC keeps rebooting, when it first loads, tried to Boot into safe mode, which I can not access, tried Windows Repair can't get in to give her password, tried making it a slave on my...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.