using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install; using System.IO; using CameraServer; namespace CameraClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string serviceFilePath = Application.StartupPath + "\\CameraServer.exe"; string serviceName = "CameraService"; //事件:安装服务 private void install_Click(object sender, EventArgs e) { try { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); MessageBox.Show(serviceName+"安装成功"); Log.writeLog(DateTime.Now + ":"+serviceName+"安装成功"); } catch (Exception ex) { MessageBox.Show(serviceName+"安装失败请查看日志"); Log.writeLog(DateTime.Now + ":"+serviceName+"安装失败," + ex.Message); } } //事件:启动服务 private void start_Click(object sender, EventArgs e) { try { if (this.IsServiceExisted(serviceName)) { this.ServiceStart(serviceName); MessageBox.Show(serviceName + "启动成功"); } else MessageBox.Show(serviceName + "不存在"); //Log.writeLog(DateTime.Now + ":GstService启动成功"); } catch (Exception ex) { MessageBox.Show(serviceName + "启动失败请查看日志"); Log.writeLog(DateTime.Now + ":" + serviceName + "启动失败," + ex.Message); } } //事件:停止服务 private void stop_Click(object sender, EventArgs e) { try { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); MessageBox.Show(serviceName+"停止成功"); //Log.writeLog(DateTime.Now + ":GstService停止成功"); } catch (Exception ex) { MessageBox.Show(serviceName+"停止失败请查看日志"); Log.writeLog(DateTime.Now + ":"+serviceName+"停止失败," + ex.Message); } } //事件:卸载服务 private void remove_Click(object sender, EventArgs e) { try { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); MessageBox.Show(serviceName + "卸载成功"); Log.writeLog(DateTime.Now + serviceName+"卸载成功"); } else { MessageBox.Show(serviceName + "卸载失败"); Log.writeLog(DateTime.Now + serviceName+"卸载失败"); } } catch (Exception ex) { MessageBox.Show("GstService卸载失败请查看日志"); Log.writeLog(DateTime.Now + ":" + serviceName + "卸载失败," + ex.Message); } } //判断服务是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安装服务 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸载服务 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //启动服务 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //停止服务 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } } } }