代码如下:

注意:先要添加引用System.ServiceProcess

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ServiceIsExisted("MSSQLSERVER"));
        }

        public static bool ServiceIsExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName == serviceName)
                {
                    return true;
                }
            }
            return false;
        }  
    }

posted on 2012-06-28 15:49  aparche  阅读(312)  评论(0编辑  收藏  举报