您的位置:首页 > 其它

Windows服务安装后设置自动启动与允许服务与桌面交互的方法

2009-12-09 08:27 831 查看
1、自动启动的方法设置:

请先设置以下两个控件:
设置serviceProcessInstaller1控件的Account属性为“LocalSystem”.
设置serviceInstaller1控件的StartType属性为"Automatic".

服务上添加安装程序后,通过编码实现自动启动,方法如下:

给serviceProcessInstaller1添加AfterInstall事件,然后添加如下代码:

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
#region Windows服务安装后自动启动
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string Cmdstring = "sc start MonitoringService"; //CMD命令,MonitoringService为服务名称,即控件serviceInstaller1的ServiceName属性。
p.StandardInput.WriteLine(Cmdstring);
p.StandardInput.WriteLine("exit");
#endregion
}


2、设置允许服务与桌面交互方法:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;
using System.Diagnostics;

namespace Systam
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}

/// <summary>
/// 设置允许服务与桌面交互 ,修改了注册表,要重启系统才能生效
/// </summary>
/// <param name="ServiceName">服务程序名称</param>
private void SetServiceTable(string ServiceName)
{
RegistryKey rk = Registry.LocalMachine;
string key = @"SYSTEM/CurrentControlSet/Services/" + ServiceName;
RegistryKey sub = rk.OpenSubKey(key, true);
int value = (int)sub.GetValue("Type");
sub.SetValue("Type", value | 256);
}

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
#region 设置允许服务与桌面交互
SetServiceTable("MonitoringService");
#endregion

#region Windows服务安装后自动启动
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string Cmdstring = "sc start MonitoringService"; //CMD命令
p.StandardInput.WriteLine(Cmdstring);
p.StandardInput.WriteLine("exit");
#endregion
}

}
}


参考:

/article/5820214.html

/article/5853914.html

3、方法3:在ProjectInstaller的视图设计器中添加控件ServiceController,即serviceController1,添加serviceInstaller1的AfterInstall事件:

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
ConnectionOptions coOptions = new ConnectionOptions();

coOptions.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope mgmtScope = new System.Management.ManagementScope(@"root/CIMV2", coOptions);

mgmtScope.Connect();

ManagementObject wmiService;

wmiService = new ManagementObject("Win32_Service.Name='" + serviceController1.ServiceName + "'");

ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");

InParam["DesktopInteract"] = true;

ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);

serviceController1.Start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐