在.NET中自行安装Windows服务c#

我已经阅读了这个问题Inno安装Windows服务? 。 我有同样的问题,但我不明白从lubos hasko的答案。 我到底该怎么办? 你可以有人张贴我完整演练吗?

当我运行下面的代码,安装了一些东西,但在服务列表中,我找不到它。 我有这个,但是这不起作用:

using System; using System.Collections.Generic; using System.Configuration.Install; using System.Linq; using System.Reflection; using System.ServiceProcess; using System.Text; using System.IO; namespace ConsoleApplication1 { public class Service1 : ServiceBase { public Service1() { File.AppendAllText("sss.txt", "ccccc"); } protected override void OnStart(string[] args) { File.AppendAllText("sss.txt", "asdfasdf"); } protected override void OnStop() { File.AppendAllText("sss.txt", "bbbbb"); } static void Main(string[] args) { if (System.Environment.UserInteractive) { string parameter = string.Concat(args); switch (parameter) { case "--install": ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); break; case "--uninstall": ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); break; } } else { ServiceBase.Run(new Service1()); } Console.ReadKey(); } } } 

我也不知道这个:

  if (System.Environment.UserInteractive) ... 

这是我完整的解决scheme,它的工作原理。 这个问题基本上是一样的答案。

 using System; using System.Configuration.Install; using System.Reflection; using System.ServiceProcess; using System.IO; namespace ConsoleApplication1 { class Program : ServiceBase { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; if (System.Environment.UserInteractive) { string parameter = string.Concat(args); switch (parameter) { case "--install": ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); break; case "--uninstall": ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); break; } } else { ServiceBase.Run(new Program()); } } private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) { File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message); } public Program() { this.ServiceName = "My Service"; File.AppendAllText(@"C:\Temp\sss.txt", "aaa"); } protected override void OnStart(string[] args) { base.OnStart(args); File.AppendAllText(@"C:\Temp\sss.txt", "bbb"); } protected override void OnStop() { base.OnStop(); File.AppendAllText(@"C:\Temp\sss.txt", "ccc"); } } } 

并在同一个项目中创build这个类:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using System.ServiceProcess; using System.Text; namespace ConsoleApplication1 { [RunInstaller(true)] public class MyWindowsServiceInstaller : Installer { public MyWindowsServiceInstaller() { var processInstaller = new ServiceProcessInstaller(); var serviceInstaller = new ServiceInstaller(); //set the privileges processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.DisplayName = "My Service"; serviceInstaller.StartType = ServiceStartMode.Automatic; //must be the same as what was set in Program's constructor serviceInstaller.ServiceName = "My Service"; this.Installers.Add(processInstaller); this.Installers.Add(serviceInstaller); } } } 

使用参数运行此程序–install / – 在Windows 7上以pipe理员身份进行卸载。检查温度错误日志。 检查相同path上的工作日志。

首先,在你的Service1的构造函数中设置ServiceName属性。

摘自MSDN :

您需要在构造函数中为从ServiceBaseinheritance的类实现的最小值是在组件上设置ServiceName。 在构造函数中没有其他处理是特别需要的。 你应该在OnStart中处理大多数初始化,而不是在构造函数中。

其次,在从命令行运行时,需要将parameter passing给您的服务。 --install进行安装, – --uninstall进行卸载 – 查看你的switch语句是在input参数上进行的。

System.Environment.UserInteractive属性告诉你,无论是Windows进程还是像没有用户界面的IIS运行的服务。

如果此属性为false,则不显示模式对话框或消息框,因为没有供用户交互的graphics用户界面。 资源

检查这篇文章以及。