public static ServiceSection GetConfig()
        {
            ServiceSection configSection = (ServiceSection)ConfigurationManager.GetSection("ServiceSection");

            if (configSection == null)
            {
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            }
            return(configSection);
        }
        public static ServiceSection GetConfig(string configPath)
        {
            var fileMap = new ExeConfigurationFileMap()
            {
                ExeConfigFilename = configPath
            };
            var            config        = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            ServiceSection configSection = (ServiceSection)config.GetSection("ServiceSection");

            if (configSection == null)
            {
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            }
            return(configSection);
        }
        /// <summary>
        /// 获取指定的配置对象
        /// </summary>
        /// <param name="keyName">配置名称</param>
        /// <returns></returns>
        public static TheKeyValue GetTheKeyValueCollection(string keyName)
        {
            ServiceSection configSection = (ServiceSection)ConfigurationManager.GetSection("ServiceSection");

            if (configSection == null)
            {
                throw new ConfigurationErrorsException("Section [ServiceSection] is not found.");
            }
            foreach (TheKeyValue item in configSection.KeyValues)
            {
                if (item.Name == keyName)
                {
                    return(item);
                }
            }
            return(null);
        }
示例#4
0
        /// <summary>
        /// 加载服务
        /// </summary>
        private void LoadServerData()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ServiceCode", typeof(string));
                dt.Columns.Add("ServiceName", typeof(string));
                dt.Columns.Add("ServiceState", typeof(string));
                dt.Columns.Add("ServicePath", typeof(string));

                #region 加载基础服务
                ServiceSection serviceSection = ServiceSection.GetConfig();
                foreach (TheKeyValue item in serviceSection.KeyValues)
                {
                    DataRow tbsRow = dt.NewRow();
                    tbsRow["ServiceCode"] = item.Name;
                    tbsRow["ServiceName"] = item.Text;
                    tbsRow["ServicePath"] = Application.StartupPath + item.Path;
                    if (Windows.isServiceIsExisted(item.Name))
                    {
                        ////判断是否服务启动状态
                        if (Windows.IsRunning(item.Name))
                        {
                            tbsRow["ServiceState"] = "服务正在运行";
                        }
                        else
                        {
                            tbsRow["ServiceState"] = "服务已停止";
                        }
                    }
                    else
                    {
                        tbsRow["ServiceState"] = "服务已卸载";
                    }
                    dt.Rows.Add(tbsRow);
                }
                //绑定数据
                ServiceList.DataSource = dt;
                #endregion



                #region 格式化一下行颜色
                for (int i = 0; i < ServiceList.RowCount; i++)
                {
                    switch (ServiceList.Rows[i].Cells["ServiceState"].Value.ToString())
                    {
                    case "服务正在运行":
                        ServiceList.Rows[i].DefaultCellStyle.BackColor = btnOpenService.BackColor;
                        break;

                    case "服务已停止":
                    case "服务已卸载":
                        ServiceList.Rows[i].DefaultCellStyle.BackColor = btnUninstall.BackColor;
                        break;

                    default:
                        break;
                    }
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }