在C#中停止和启动IIS

编程入门 行业动态 更新时间:2024-10-10 04:26:34
本文介绍了在C#中停止和启动IIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要能够在C#中启动/停止IIS.我的经理已负责创建此模块.以下是我的尝试:

I need to be able to Start/Stop the IIS in C#. I've been tasked with creating this module by my manager. The following is my attempt:

ServiceController service = new ServiceController("w3svc"); service.Stop();

我遇到以下异常:

无法在计算机."上打开w3svc服务.

Cannot open w3svc service on computer '.'.

我正在编写代码来停止本地计算机上的IIS.

I am writing the code to stop the IIS on the local machine.

我也尝试使用IISAdmin作为服务名,但是在我的计算机上找不到IISAdmin.

I also tried IISAdmin as the servicename, but IISAdmin could not be found on my computer.

推荐答案

您必须使用 Microsoft.Web.Administration .

Microsoft.Web.Administration(MWA)API被构建为托管 应用程序主机管理API(AHADMIN)上的代码包装 这是一个本机代码接口库.它提供了程序化 访问和更新Web服务器配置的方式以及 管理信息.

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. It provides a programmatic way to access and update the web server configuration and administration information.

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

此文章讨论了有关以下内容的详细方案使用Microsoft.Web.Administration.

This article discuss detailed scenarios for using Microsoft.Web.Administration.

请注意,您必须以管理员身份运行C#应用程序才能在IIS上执行任何操作.否则,您可能会被拒绝访问.

更多推荐

在C#中停止和启动IIS

本文发布于:2023-11-27 12:45:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IIS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!