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

来源:互联网 发布:淘宝可以删差评吗 编辑:程序博客网 时间:2024/05/21 06:47

1、自动启动的方法设置:

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

 

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

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

 

[c-sharp] view plaincopy
  1. private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)  
  2. {  
  3.     #region Windows服务安装后自动启动  
  4.     Process p = new Process();  
  5.     p.StartInfo.FileName = "cmd.exe";  
  6.     p.StartInfo.UseShellExecute = false;  
  7.     p.StartInfo.RedirectStandardInput = true;  
  8.     p.StartInfo.RedirectStandardOutput = true;  
  9.     p.StartInfo.RedirectStandardError = true;  
  10.     p.StartInfo.CreateNoWindow = true;  
  11.     p.Start();  
  12.     string Cmdstring = "sc start MonitoringService"//CMD命令,MonitoringService为服务名称,即控件serviceInstaller1的ServiceName属性。  
  13.     p.StandardInput.WriteLine(Cmdstring);  
  14.     p.StandardInput.WriteLine("exit");  
  15.     #endregion  
  16. }  

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

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Configuration.Install;  
  5. using Microsoft.Win32;  
  6. using System.Diagnostics;  
  7.   
  8. namespace Systam  
  9. {  
  10.     [RunInstaller(true)]  
  11.     public partial class ProjectInstaller : Installer  
  12.     {  
  13.         public ProjectInstaller()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         /// <summary>  
  19.         /// 设置允许服务与桌面交互 ,修改了注册表,要重启系统才能生效  
  20.         /// </summary>  
  21.         /// <param name="ServiceName">服务程序名称</param>  
  22.         private void SetServiceTable(string ServiceName)  
  23.         {  
  24.             RegistryKey rk = Registry.LocalMachine;  
  25.             string key = @"SYSTEM/CurrentControlSet/Services/" + ServiceName;  
  26.             RegistryKey sub = rk.OpenSubKey(key, true);  
  27.             int value = (int)sub.GetValue("Type");  
  28.             sub.SetValue("Type", value | 256);  
  29.         }  
  30.   
  31.         private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)  
  32.         {  
  33.             #region 设置允许服务与桌面交互  
  34.             SetServiceTable("MonitoringService");  
  35.             #endregion  
  36.  
  37.             #region Windows服务安装后自动启动  
  38.             Process p = new Process();  
  39.             p.StartInfo.FileName = "cmd.exe";  
  40.             p.StartInfo.UseShellExecute = false;  
  41.             p.StartInfo.RedirectStandardInput = true;  
  42.             p.StartInfo.RedirectStandardOutput = true;  
  43.             p.StartInfo.RedirectStandardError = true;  
  44.             p.StartInfo.CreateNoWindow = true;  
  45.             p.Start();  
  46.             string Cmdstring = "sc start MonitoringService"//CMD命令  
  47.             p.StandardInput.WriteLine(Cmdstring);  
  48.             p.StandardInput.WriteLine("exit");  
  49.             #endregion  
  50.         }  
  51.   
  52.     }  
  53. }  

 

参考:

http://www.cnblogs.com/joejoe/archive/2009/05/09/1453250.html

http://www.cnblogs.com/wfwup/archive/2009/01/14/1375382.html

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

[c-sharp] view plaincopy
  1. private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)  
  2. {  
  3.     ConnectionOptions coOptions = new ConnectionOptions();  
  4.   
  5.     coOptions.Impersonation = ImpersonationLevel.Impersonate;  
  6.   
  7.     ManagementScope mgmtScope = new System.Management.ManagementScope(@"root/CIMV2", coOptions);  
  8.   
  9.     mgmtScope.Connect();  
  10.   
  11.     ManagementObject wmiService;  
  12.   
  13.     wmiService = new ManagementObject("Win32_Service.Name='" + serviceController1.ServiceName + "'");  
  14.   
  15.     ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");  
  16.   
  17.     InParam["DesktopInteract"] = true;  
  18.   
  19.     ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);  
  20.   
  21.     serviceController1.Start();     
  22. }  

原创粉丝点击