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