在.NET中自行安装Windows服务

编程入门 行业动态 更新时间:2024-10-27 12:27:28
本文介绍了在.NET中自行安装Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已阅读此问题。我有同样的问题,但我不明白lubos hasko的答案。我到底该怎么做?

I have read this question. I have same issue, but I don't understand the answer from lubos hasko. How exactly can I do it? Can you someone post me full walkthrough?

当我在下面运行代码时,已经安装了某些东西,但是在服务列表中找不到它。

When I run code below, something is installed, but in list of service, I could not find it.

我有这个,但这不起作用:

I have this, but this not work:

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) ...

推荐答案

这是我完整的解决方案,并且可以使用。基本上与此问题中的答案相同。

This is my complete solution, and it works. It is basically the same answer as in this question.

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"); } } }

,并在同一项目中创建类:

and in same project create this class:

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上以管理员身份卸载。检查temp中的错误日志。在同一路径上检查工作日志。

Run this program with parameters --install/--uninstall as Administrator on Windows 7. Check the error log in temp. Check working log on the same path.

更多推荐

在.NET中自行安装Windows服务

本文发布于:2023-11-14 16:44:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!