检查IIS是否已安装并正在运行

时间:2012-03-22 05:09:11

标签: c# asp.net iis

在我们的应用程序中,我们想要确定iis是否安装在计算机中。如果已安装,那么我们需要确定它是否正在运行。

有没有办法获得这些细节。

1 个答案:

答案 0 :(得分:10)

Using Managed Code to Detect if IIS is Installed and ASP/ASP.NET is Registered

IIS运行与否检查以下代码

只需在项目中添加“System.ServiceProcess”参考。

    ServiceController sc = new ServiceController("World Wide Web Publishing Service");
if ((sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending))) {
    Console.WriteLine("Starting the service...");
    sc.Start();
}
else {
    Console.WriteLine("Stopping the service...");
    sc.Stop();
}
相关问题