|
- using CnasSynchronousCommon;
- using CnasSynchrousModel;
- using Microsoft.Win32;
- using System;
- using System.Collections;
- using System.Configuration.Install;
- using System.IO;
- using System.ServiceProcess;
- using System.Windows.Forms;
- using System.Xml.Serialization;
-
- namespace CNAS_DBSync
- {
- public partial class frmServiceConfig : Form
- {
- ServiceConfig serviceConfig = new ServiceConfig();
- public frmServiceConfig()
- {
- InitializeComponent();
- }
-
- private void frmServiceConfig_Load(object sender, EventArgs e)
- {
- //1.读取本地配置文件,填充上次配置时间
- try
- {
- XmlSerializer serializer = new XmlSerializer(serviceConfig.GetType());
- FileStream stream = new FileStream(FileHelper.getBasePath() + "/DataConfig/SyncServiceData.xml", FileMode.Open);
- serviceConfig = (ServiceConfig)serializer.Deserialize(stream);
- stream.Close();
-
- this.numService.Value = serviceConfig.Minutes;
- this.dtpSource.Value = serviceConfig.InitalDT;
-
- this.cbxAutoStart.Checked = getServiceStartType("SyncService") == 2 ? true : false;
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- }
- //2.检查本地是否存在已经启动的服务,初始化btn的显示
- string strMsg = IfStartService();
- switch (strMsg)
- {
- case "1":
- btnInstallService.Text = "安装服务";
- btnRun.Text = "启动服务";
- this.numService.Enabled = true;
- break;
- case "2":
- btnInstallService.Text = "卸载服务";
- btnRun.Text = "启动服务";
- this.numService.Enabled = true;
- break;
- case "3":
- btnInstallService.Text = "卸载服务";
- btnRun.Text = "停止服务";
- this.numService.Enabled = false;
- break;
- }
- }
-
- private string IfStartService()
- {
- ServiceController[] service = ServiceController.GetServices();
- string strMsg = "1";
- for (int i = 0; i < service.Length; i++)
- {
- if (service[i].DisplayName.ToUpper().Equals("SyncService".ToUpper()))
- {
- strMsg = "2";
- if (service[i].Status == ServiceControllerStatus.Running)
- {
- strMsg = "3";
- break;
- }
- }
- }
- return strMsg;
- }
-
- private void btnRun_Click(object sender, EventArgs e)
- {
- if (btnRun.Text == "启动服务") //此时启动服务
- {
- if (this.numService.Value < 1)
- {
- MessageBox.Show("服务至少每隔1分钟执行");
- return;
- }
- serviceConfig.Minutes = (Int32)this.numService.Value;
-
- serviceConfig.InitalDT = this.dtpSource.Value;
- //修改服务为开启启动
- if(cbxAutoStart.Checked)
- ChangeServiceStartType(2, "SyncService");
- if (StartService())
- {
- SaveServiceConfig(serviceConfig); //存储配置文件
- btnRun.Text = "停止服务";
- }
- else
- {
- MessageBox.Show("启动服务失败!");
- }
- }
- else //此时停止服务
- {
- if (StopService())
- {
- if (cbxAutoStart.Checked)
- {
- ChangeServiceStartType(3, "SyncService");
- cbxAutoStart.Checked = false;
- }
- btnRun.Text = "启动服务";
- }
- else
- {
- MessageBox.Show("停止服务失败!");
- }
- }
- }
- private bool StartService()
- {
- bool bIfSuccess = true;
- try
- {
- ServiceController serviceController = new ServiceController("SyncService");
- serviceController.Start();
- serviceController.WaitForStatus(ServiceControllerStatus.Running);
- serviceController.Close();
- }
- catch (Exception ex)
- {
- bIfSuccess = false;
- AppLog.Error(ex.Message);
- }
- return bIfSuccess;
- }
- private bool StopService()
- {
- bool bIfSuccess = true;
- try
- {
- ServiceController serviceController = new ServiceController("SyncService");
- serviceController.Stop();
- serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
- serviceController.Close();
- }
- catch (Exception ex)
- {
- bIfSuccess = false;
- AppLog.Error(ex.Message);
- }
- return bIfSuccess;
- }
-
- private void SaveServiceConfig(ServiceConfig config)
- {
- try
- {
- XmlSerializer serializer = new XmlSerializer(config.GetType());
- TextWriter writer = new StreamWriter(FileHelper.getBasePath() + "/DataConfig/SyncServiceData.xml");
- serializer.Serialize(writer, config);
- writer.Close();
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- }
- }
-
- /// <summary>
- /// 修改服务的启动项 2为自动,3为手动
- /// </summary>
- /// <param name="startType"></param>
- /// <param name="serviceName"></param>
- /// <returns></returns>
- public static bool ChangeServiceStartType(int startType, string serviceName)
- {
- try
- {
- RegistryKey regist = Registry.LocalMachine;
- RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
- RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
- RegistryKey services = currentControlSet.OpenSubKey("Services");
- RegistryKey servicesName = services.OpenSubKey(serviceName, true);
- servicesName.SetValue("Start", startType);
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- return false;
- }
- return true;
- }
-
- /// <summary>
- /// 获取服务启动类型
- /// </summary>
- /// <param name="serviceName"></param>
- /// <returns></returns>
- public static int getServiceStartType(string serviceName)
- {
- int startType = 0;
- try
- {
- RegistryKey regist = Registry.LocalMachine;
- RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
- RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
- RegistryKey services = currentControlSet.OpenSubKey("Services");
- RegistryKey servicesName = services.OpenSubKey(serviceName, true);
- if(servicesName != null)
- startType=(int)servicesName.GetValue("Start");
- }
- catch (Exception ex)
- {
- AppLog.Error(ex.Message);
- return 0;
- }
- return startType;
- }
-
- private void btnInstallService_Click(object sender, EventArgs e)
- {
- string serviceFilePath = FileHelper.getBasePath() + @"\CNAS_SyncService.exe";
- if (btnInstallService.Text == "安装服务")
- {
- if (InstallService(serviceFilePath))
- {
- btnInstallService.Text = "卸载服务";
- }
- else
- {
- MessageBox.Show("安装服务失败。");
- }
- }
- else if (btnInstallService.Text == "卸载服务")
- {
- if (UninstallService(serviceFilePath))
- {
- btnInstallService.Text = "安装服务";
- btnRun.Text = "启动服务";
- }
- else
- {
- MessageBox.Show("卸载服务失败。");
- }
- }
- }
-
- /// <summary>
- /// 安装服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private bool InstallService(string serviceFilePath)
- {
- bool bIfSuccess = true;
- try
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- IDictionary savedState = new Hashtable();
- installer.Install(savedState);
- installer.Commit(savedState);
- }
- }
- catch (Exception ex)
- {
- bIfSuccess = false;
- AppLog.Error(ex.Message);
- }
- return bIfSuccess;
- }
-
- /// <summary>
- /// 卸载服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private bool UninstallService(string serviceFilePath)
- {
- bool bIfSuccess = true;
- try
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- catch (Exception ex)
- {
- bIfSuccess = false;
- AppLog.Error(ex.Message);
- }
- return bIfSuccess;
- }
- }
- }
|