64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
using System.ServiceProcess;
|
|
|
|
namespace HL_RFIDAutomaticInductionTime
|
|
{
|
|
[RunInstaller(true)]
|
|
public partial class ProjectInstaller : System.Configuration.Install.Installer
|
|
{
|
|
/// <summary>
|
|
/// 初始化配置文件
|
|
/// </summary>
|
|
Config config = new Config();
|
|
|
|
public ProjectInstaller()
|
|
{
|
|
InitializeComponent();
|
|
this.serviceInstaller1.ServiceName = config.ServiceName;
|
|
this.serviceInstaller1.Description = config.ServiceDescription;
|
|
this.serviceInstaller1.DisplayName = config.DisplayName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置服务安装后“允许和桌面进行交互”
|
|
/// </summary>
|
|
/// <param name="savedState"></param>
|
|
protected override void OnAfterInstall(IDictionary savedState)
|
|
{
|
|
try
|
|
{
|
|
base.OnAfterInstall(savedState);
|
|
|
|
// 允许服务桌面交互
|
|
System.Management.ManagementObject myService = new System.Management.ManagementObject(string.Format("Win32_Service.Name='{0}'", this.serviceInstaller1.ServiceName));
|
|
|
|
System.Management.ManagementBaseObject changeMethod = myService.GetMethodParameters("Change");
|
|
|
|
changeMethod["DesktopInteract"] = true;
|
|
|
|
System.Management.ManagementBaseObject OutParam = myService.InvokeMethod("Change", changeMethod, null);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重写ProjectInstaller的Commit方法
|
|
/// </summary>
|
|
/// <param name="savedState"></param>
|
|
public override void Commit(IDictionary savedState)
|
|
{
|
|
base.Commit(savedState);
|
|
ServiceController sc = new ServiceController(config.ServiceName);
|
|
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
|
|
{
|
|
sc.Start();
|
|
}
|
|
}
|
|
}
|
|
}
|