[英]Stopping and Starting IIS in C#
I need to be able to Start/Stop the IIS in C#. 我需要能够在C#中启动/停止IIS。 I've been tasked with creating this module by my manager.
我的经理已负责创建此模块。 The following is my attempt:
以下是我的尝试:
ServiceController service = new ServiceController("w3svc");
service.Stop();
I am getting the following exception: 我收到以下异常:
Cannot open w3svc service on computer '.'.
无法在计算机“。”上打开w3svc服务。
I am writing the code to stop the IIS on the local machine. 我正在编写代码以停止本地计算机上的IIS。
I also tried IISAdmin
as the servicename, but IISAdmin
could not be found on my computer. 我也尝试使用
IISAdmin
作为服务名,但是在我的计算机上找不到IISAdmin
。
You have to work with Microsoft.Web.Administration . 您必须使用Microsoft.Web.Administration 。
The Microsoft.Web.Administration (MWA) APIs are built as a managed code wrapper over the Application Host Administration API (AHADMIN) which is a native code interface library.
Microsoft.Web.Administration(MWA)API作为应用程序宿主管理API(AHADMIN)(作为本机代码接口库)构建为托管代码包装器。 It provides a programmatic way to access and update the web server configuration and administration information.
它提供了一种编程方式来访问和更新Web服务器配置和管理信息。
using System;
using System.Linq;
using Microsoft.Web.Administration;
class Program
{
static void Main(string[] args)
{
var server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
if (site != null)
{
//stop the site
site.Stop();
//start the site
site.Start();
}
}
}
This article discuss detailed scenarios for using Microsoft.Web.Administration. 此文章使用Microsoft.Web.Administration讨论的详细情形。
Please note that you have to run your C# application as Administrator to do anything on IIS.Otherwise you may get Access denied. 请注意,您必须以管理员身份运行C#应用程序才能在IIS上执行任何操作,否则可能会导致访问被拒绝。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.