Windows服务允许服务使用界面交互(C#/ProjectInstaller)
Windows服务允许服务使用界面交互(C#/ProjectInstaller)
参考:ProjectInstaller.cs 代码
C# 全选
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
// 设置运行该服务应用程序时所使用的帐户类型,(默认account,服务安装的时候会提示输入用户名密码)
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
// 设定服务名称
this.serviceInstaller1.ServiceName = "MyService";
this.serviceInstaller1.DisplayName = "MyService";
this.serviceInstaller1.Description = "我的Win服务";
// 设定服务的启动方式 自动启动
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.AfterInstall += ProjectInstaller_AfterInstall;
}
/// <summary>
/// 允许服务使用界面交互
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
SetServiceDesktopInsteract(this.serviceInstaller1.ServiceName);
}
/// <summary>
/// 允许服务使用界面交互
/// </summary>
/// <param name="serviceName"></param>
private void SetServiceDesktopInsteract(string serviceName)
{
ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
changeMethod["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网