3. 安装, 启动, 停止, 卸载服务
实现代码:
- private string ServicePath => txtServicePath.Text.Trim();
- private string ServiceName => "ServiceSample";
- private void BtnStart_Click(object sender, EventArgs e)
- {
- if (!ServiceHelper.IsExisted(ServiceName))
- {
- MessageBoxHelper.ShowError($"{ServiceName} 不存在");
- return;
- }
- ServiceHelper.Start(ServiceName);
- }
- private void BtnStop_Click(object sender, EventArgs e)
- {
- if (!ServiceHelper.IsExisted(ServiceName))
- {
- MessageBoxHelper.ShowError($"{ServiceName} 不存在");
- return;
- }
- ServiceHelper.Stop(ServiceName);
- }
- private void BtnInstall_Click(object sender, EventArgs e)
- {
- if (ServiceHelper.IsExisted(ServiceName))
- {
- MessageBoxHelper.ShowError($"{ServiceName} 已经存在");
- return;
- }
- ServiceHelper.Install(ServicePath);
- }
- private void BtnUnInstall_Click(object sender, EventArgs e)
- {
- if (!ServiceHelper.IsExisted(ServiceName))
- {
- MessageBoxHelper.ShowError($"{ServiceName} 不存在");
- return;
- }
- ServiceHelper.Uninstall(ServicePath);
- }
- }
来源: https://www.2cto.com/kf/201905/809339.html