コード例 #1
0
        public List <ServiceStateInfo> GetServiceStates()
        {
            List <ServiceStateInfo> list = new List <ServiceStateInfo>();
            string lastAction            = "";

            try
            {
                lastAction = "Getting service states on " + MachineName;
                ServiceController[] allServices = ServiceController.GetServices(MachineName);
                foreach (ServiceController srvc in (from s in allServices
                                                    where SubItems.Exists(sub => sub.Description == s.DisplayName)
                                                    select s))
                {
                    lastAction = string.Format("Getting service state for {0}\\{1}", MachineName, srvc.DisplayName);
                    list.Add(new ServiceStateInfo()
                    {
                        DisplayName = srvc.DisplayName, Status = srvc.Status
                    });
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("GetServiceStates Error: " + ex.ToString());
                throw;
            }
            return(list);
        }