示例#1
0
        private void InitService()
        {
            LB.RMT.Base.ConfigHelper.ReadIniConfig();

            // 服务名称
            _strService_M3            = LB.RMT.Base.ConfigHelper.ServiceName;
            _strService_WebAutoUpdate = _strService_M3 + "_WebAutoUpdate";
            _strService_DBAutoBackUp  = _strService_M3 + "_DBAutoBackUp";

            txtServiceName.Text = _strService_M3;

            // 服务路径
            mstrPath  = System.Reflection.Assembly.GetExecutingAssembly().Location;
            mstrPath  = mstrPath.Substring(0, mstrPath.LastIndexOf(@"\"));                      // 去掉文件名,得到目录
            mstrPath += (mstrPath.EndsWith(@"\") ? "" : @"\");

            _strServicePath_M3            = mstrPath + "LB.Web.Server.exe";
            _strServicePath_WebAutoUpdate = mstrPath + @"WebAutoUpdate\TS.DWWeb.WebAutoUpdateServer.exe";
            _strServicePath_DBAutoBackUp  = mstrPath + @"ServerBak\TS.ServerBak.DBAutoBackUpServer.exe";

            // 服务帮助类对象
            _sh_M3            = new ServiceTool.ServiceHelper(_strService_M3, _strServicePath_M3);
            _sh_WebAutoUpdate = new ServiceTool.ServiceHelper(_strService_WebAutoUpdate, _strServicePath_WebAutoUpdate);
            _sh_DBAutoBackUp  = new ServiceTool.ServiceHelper(_strService_DBAutoBackUp, _strServicePath_DBAutoBackUp);
        }
示例#2
0
        private string SaveInstallServices()
        {
            string strServiceName = txtServiceName.Text.Trim();

            if (strServiceName == string.Empty)
            {
                return("服务名不能为空");
            }

            StringBuilder sbMsg = new StringBuilder();

            bool bChangeServiceName = strServiceName != LB.RMT.Base.ConfigHelper.ServiceName;

            string strServiceName_Old = LB.RMT.Base.ConfigHelper.ServiceName;

            try
            {
                LB.RMT.Base.ConfigHelper.ServiceName = strServiceName;
                LB.RMT.Base.ConfigHelper.WriteIniConfig();
                LB.RMT.Base.ConfigHelper.ReadIniConfig();
            }
            catch (Exception ex)
            {
                sbMsg.Append(ex.Message);
                sbMsg.AppendLine();
                return(sbMsg.ToString());
            }

            string strName = string.Empty;

            try
            {
                for (int i = 0; i < clbService.Items.Count; i++)
                {
                    string item = clbService.Items[i].ToString();

                    string strService = string.Empty;
                    ServiceTool.ServiceHelper sh_New;
                    ServiceTool.ServiceHelper sh_Old;

                    if (item.StartsWith("M3主服务"))
                    {
                        strName    = "M3主服务";
                        strService = LB.RMT.Base.ConfigHelper.ServiceName;
                        sh_Old     = _sh_M3;
                        sh_New     = new ServiceTool.ServiceHelper(strService, _strServicePath_M3);
                    }
                    else if (item.StartsWith("自动更新服务"))
                    {
                        strName    = "自动更新服务";
                        strService = LB.RMT.Base.ConfigHelper.ServiceName + "_WebAutoUpdate";
                        sh_Old     = _sh_WebAutoUpdate;
                        sh_New     = new ServiceTool.ServiceHelper(strService, _strServicePath_WebAutoUpdate);
                    }
                    else if (item.StartsWith("自动备份服务"))
                    {
                        strName    = "自动备份服务";
                        strService = LB.RMT.Base.ConfigHelper.ServiceName + "_DBAutoBackUp";
                        sh_Old     = _sh_DBAutoBackUp;
                        sh_New     = new ServiceTool.ServiceHelper(strService, _strServicePath_DBAutoBackUp);
                    }
                    else
                    {
                        continue;
                    }

                    bool b = clbService.GetItemChecked(i);

                    if (bChangeServiceName || sh_Old.ServiceName != sh_New.ServiceName)                         // 修改了服务名,所有服务应该重新安装
                    {
                        if (sh_Old.ExistsService())
                        {
                            sh_Old.Uninstall();
                            sh_New.Install();
                            sh_New.StartService();
                        }
                        else if (b)
                        {
                            sh_New.Install();
                            sh_New.StartService();
                        }

                        if (item.StartsWith("M3主服务"))
                        {
                            _sh_M3         = sh_New;
                            _strService_M3 = strService;
                        }
                        else if (item.StartsWith("自动更新服务"))
                        {
                            _sh_WebAutoUpdate         = sh_New;
                            _strService_WebAutoUpdate = strService;
                        }
                        else if (item.StartsWith("自动备份服务"))
                        {
                            _sh_DBAutoBackUp         = sh_New;
                            _strService_DBAutoBackUp = strService;
                        }
                    }
                    else
                    {
                        if (!b)
                        {
                            continue;
                        }

                        if (sh_New.ExistsService())
                        {
                            continue;
                        }

                        sh_New.Install();
                        sh_New.StartService();

                        if (item.StartsWith("M3主服务"))
                        {
                            _sh_M3         = sh_New;
                            _strService_M3 = strService;
                        }
                        else if (item.StartsWith("自动更新服务"))
                        {
                            _sh_WebAutoUpdate         = sh_New;
                            _strService_WebAutoUpdate = strService;
                        }
                        else if (item.StartsWith("自动备份服务"))
                        {
                            _sh_DBAutoBackUp         = sh_New;
                            _strService_DBAutoBackUp = strService;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                sbMsg.Append(string.Format("服务({0})安装失败:{1}", strName, ex.Message));
                sbMsg.AppendLine();
            }

            return(sbMsg.ToString());
        }