在项目中,特别是安装项目中我们经常要判断一些服务是否启动(判断SQL Server是否启动最常见),在.net中我们如何判断指定的Windows服务是否启动呢?首先要知道Windows服务的显示名称,这里以IIS检测为例,我们知道IIS的显示名称是"IIS Admin",现在用下面的代码来判断IIS是否启动。
ServiceController[] service=ServiceController.GetServices();
                        bool isStart = false;
                        for (int i = 0; i < service.Length; i++)
                        ...{
                                if (service[i].DisplayName.ToUpper().Equals("IIS Admin".ToUpper()))
                                ...{
                                        if (service[i].Status == ServiceControllerStatus.Running)
                                        ...{
                                                isStart = true;
                                                break;
                                     }            
                                }
                        }
                        if (isStart)
                        ...{
                                MessageBox.Show("服务已经启动");
                        }
                        else
                        ...{
                                MessageBox.Show("服务没启动");
                        }
在使用时别忘记导入System.ServiceProcess这个名称空间,一般情况下VS.net是不会自动导入的。