【发布时间】:2012-07-02 21:23:32
【问题描述】:
我正在寻找停止/启动 IIS 网站的 c# .Net 应用程序(Web 或桌面)。
我在互联网上搜索了这方面的内容,但找不到有效的示例或解释。
我找到了这个 dll http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager%28v=VS.90%29.aspx
这是我正在尝试停止/启动 IIS 网站的示例:
“使用 Microsoft.Web.Administration”参考..
var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == "SITENAME");
if (site != null)
{
//stop the site...
site.Stop();
if (site.State == ObjectState.Stopped)
{
//do deployment tasks...
}
else
{
throw new InvalidOperationException("Could not stop website!");
}
//restart the site...
site.Start();
}
else
{
throw new InvalidOperationException("Could not find website!");
}
site.Stop 抛出异常:
"Access denied. (Excepción de HRESULT: 0x80070005 (E_ACCESSDENIED))"
ExceptionType "UnauthorizedAccessException"
我该怎么做?我做错了什么?
【问题讨论】:
-
错误似乎很明显,您的应用程序运行的安全上下文没有权限。如果您的程序本身在 IIS 下运行,请确保它使用的应用程序池具有特权用户帐户。
-
您可以使用“以管理员身份运行”运行应用程序,或者以编程方式执行权限提升过程。在我看来,这是一个很好的起点:msdn.microsoft.com/en-us/library/bb756996。