在C#和ASP.NET中关闭或重新启动计算机 [英] Shutdown or Restart Machine In C# and ASP.NET

查看:72
本文介绍了在C#和ASP.NET中关闭或重新启动计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够关闭或重新启动运行我的ASP.NET应用程序的服务器。下面的代码在调试模式下效果很好:

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();

我也尝试过此代码,但是收到以下错误消息: Process对象必须具有UseShellExecute属性设置为false以便以用户身份启动进程:

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;

然后出现错误 Process对象必须将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

在调试模式下执行代码或运行命令是cmd。但是,当我在服务器上实时运行该应用程序时,它不会重新启动。我没有收到任何错误,或突然说服务器将要重启。有人可以告诉我我在做什么错吗?

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?

更新:我添加了 try-catch ,应用程序永远不会引发异常。但是,当我查看事件日志时,我发现了shutdown.exe的应用程序错误事件1000

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

推荐答案

您的应用程序池正在运行,没有足够的特权来重新引导系统,这就是为什么它不会发生。

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.

这篇关于在C#和ASP.NET中关闭或重新启动计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆