using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Windows.Forms; namespace POSV.ServiceManager { public partial class ServiceSetting : Form { public ServiceSetting() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); string Installbat_content = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe {0} Net Start POSVD sc config POSVD start = auto"; string directory = Path.Combine(System.Environment.CurrentDirectory, ""); File.WriteAllText(Path.Combine(directory, "install.bat"), string.Format(Installbat_content, Path.Combine(directory, "POSVD.exe"))); string Unistallbat_content = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u {0}"; File.WriteAllText(Path.Combine(directory, "uninstall.bat"), string.Format(Unistallbat_content, Path.Combine(directory, "POSVD.exe"))); } private void ShowState(string message) { textBox1.Text = message; } private void btnInstallService_Click(object sender, EventArgs e) { ShowState("开始安装服务"); try { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory; ShowState("查找服务目录"); Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "install.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; ShowState("服务安装完成。"); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) msg += ex.InnerException.Message; ShowState("安装服务出现错误:" + msg); } } private void btnUninstallService_Click(object sender, EventArgs e) { ShowState("开始卸载服务"); try { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory; Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "uninstall.bat"; process.StartInfo.CreateNoWindow = true; process.Start(); System.Environment.CurrentDirectory = CurrentDirectory; ShowState("服务卸载完成。"); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) msg += ex.InnerException.Message; ShowState("卸载服务出现错误:" + msg); } } private void btnStartService_Click(object sender, EventArgs e) { try { ShowState("启动服务开始"); ServiceController serviceController = new ServiceController("POSVD"); serviceController.Start(); ShowState("启动服务完成。"); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) msg += ex.InnerException.Message; ShowState("启动服务出现错误:" + msg); } } private void btnStopService_Click(object sender, EventArgs e) { try { ShowState("停止服务开始"); ServiceController serviceController = new ServiceController("POSVD"); if (serviceController.CanStop) serviceController.Stop(); ShowState("停止服务完成。"); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) msg += ex.InnerException.Message; ShowState("停止服务出现错误:" + msg); } } private void btnShowState_Click(object sender, EventArgs e) { try { ServiceController serviceController = new ServiceController("POSVD"); string Status = serviceController.Status.ToString(); ShowState("当前服务状态是:" + Status); } catch (Exception ex) { ShowState("查看状态出现错误:" + ex.Message); } } private void btnExitApp_Click(object sender, EventArgs e) { Application.Exit(); } } }