问题
I want to be able to shutdown down or restart the server that my ASP.NET app is running on. The following code works great in debug mode:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "shutdown";
startInfo.Arguments = "/r /f /t 0";
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
I have also tried this code, but I receive the following error "The Process object must have the UseShellExecute property set to false in order to start a process as a user":
var info = new ProcessStartInfo("shutdown.exe", "/r /t 0");
info.UserName = "administrator";
//A not-so-secure use of SecureString
var secureString = new SecureString();
var password = "password";
foreach (var letter in password)
{
secureString.AppendChar(letter);
}
info.Password = secureString;
var restart = new Process();
restart.StartInfo = info;
restart.Start();
I add the the following to my code:
info.UseShellExecute = false;
Then the error "The Process object must have the UseShellExecute property set to false in order to start a process as a user" goes away, but the the code executes like the first block of code
The server will restart when I execute the code in debug mode or if I run the command is cmd. However when I run the app live on the server, it will not restart. I don't receive any error, or pops saying that the server is or is not going to restart. Can some tell me please what I am doing wrong?
UPDATE: I have added a try-catch
and the app never throws an exception. However when I looked up the event logs, I have found the application error event 1000 for shutdown.exe
回答1:
The account under which your application pool is running does not have sufficient privileges to reboot the system, that's why it doesn't occur.
You can change this by logging on to the server and subsequently going to Control Panel > Administrative Tools > Local Security Policy
, expanding the Security Settings > Local Policies > User Rights Assignment
node and going to the Shut down the system
setting. Add the application pool account (or its parent group if you have a group for it) to that list.
来源:https://stackoverflow.com/questions/19324738/shutdown-or-restart-machine-in-c-sharp-and-asp-net