如何在 C# 中以编程方式安装 Windows 服务?
我的 VS 解决方案中有 3 个项目。 其中之一是 Web 应用程序,第二个是 Windows 服务,最后一个是我的 Web 应用程序的安装项目。
我想要的是在我的设置项目中安装 Web 应用程序结束时,在我的自定义操作中尝试安装我的 Windows 服务,因为那时我已经知道了程序集的位置。
I have 3 projects in my VS solution. One of them is a Web app, the second one is a Windows Service and the last one a Setup project for my Web app.
What I want is by the end of the installation of the web app in my setup project, within my custom action to try and install my windows service given that I have the location of the assembly by then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我在您重用的代码中发现了几个错误,并已修复这些错误并对其进行了一些清理。 同样,原始代码取自 这里。
如果有人发现此代码有任何问题,请告诉我!
I found several errors in the code that you reused and have fixed these and also cleaned it up a little. Again, the original code is taken from here.
Please let me know if anyone finds anything wrong with this code!
好的,这对我来说真正有用,它已经在具有不同操作系统的多台机器上进行了测试(Vista、XP、Win2k、Win2003 服务器)
代码取自 这里 所以完全归功于编写这段代码的人。
将 dll 或源文件添加到项目中后,请确保添加 ServiceTools 命名空间,然后您就可以访问一些非常方便的功能,例如...
我希望这会有所帮助。
Ok, here is what REALLY worked for me, it has been tested on multiple machines with different OS ( Vista, XP, Win2k, Win2003 server )
The code has been taken from here so full credit goes to whoever wrote this piece of code.
Once you add the dll or source file into your project make sure to add the ServiceTools namespace and then you have access to some very handy functionality such as...
I hope this helps.
请查看本文。
有时您可能想以编程方式安装 Windows 服务,但目标计算机没有 InstallUtil.exe。
添加对
System.Configuration.Install
的引用使用以下代码。
请注意,
exeFileName
是InstallerClass .exe,而不是ServiceClass .exe。卸载:
Please have a look at this article.
Sometimes you may want to install a Windows Service programmatically, but the target machine does not have InstallUtil.exe.
Add a reference to
System.Configuration.Install
Use the code below.
Note that the
exeFileName
is the InstallerClass .exe and not the ServiceClass .exe.To uninstall:
为我的服务创建安装程序类的实例(非常基本)后,我所要做的就是调用 :
来安装它并
卸载该服务。 此处,调用代码与服务可执行文件位于同一程序集中。
要通过命令行安装服务,我所要做的就是通过命令行参数将其连接到可执行文件,并测试 System.Environment.UserInteractive 来了解它是正在执行的服务还是某人尝试安装-卸载它,瞧...没有时髦的互操作东西...没有指针泄漏...
分布在两个类中的总共大约 20 行代码就成功了。
要替换 InstallUtil,只需查看 ManagedInstallerClass.InstallHelper
After creating an instance of an installer class for my service (very basic) all I had to do is to call :
to install it and
to uninstall the service. The calling code is, here, in the same assembly as the service executable.
to get the service to install through command line all I had to do is to connect this to the executable though command line arguments and test for
System.Environment.UserInteractive
to know if it is the service executing or someone trying to install-uninstall it and voila... no funky interop stuff... no pointers leaking...Total about 20 lines of code spread over two classes did the trick.
to replace InstallUtil just take a look at ManagedInstallerClass.InstallHelper
通过使用 Topshelf 项目,您可以通过调用可执行文件来安装:
Topshelf 还负责其他 Windows 服务管道。
By using the Topshelf project you can install by calling your executable:
Topshelf also takes care of other Windows Service plumbing.
因为我面临着以编程方式安装在特定用户下运行的服务的挑战。 我扩展了
InstallAndStart
方法以利用lp
和lpPassword
...虽然不多,但可能有所帮助。
Since I was faced with the challenge to install services programmatically which run under a certain user. I extended the
InstallAndStart
method to make use oflp
andlpPassword
...Not much but might help.
我使用 Lars answer 来管理我的服务,效果很好。 但我需要将参数传递给服务,我花了几个小时才弄清楚如何做到这一点。 因此,我决定发布更改后的类,以便能够使用参数启动服务:
I used Lars answer to manage my service and it works fine. But I needed to pass arguments to service and it took me a couple of hours to find out how to do it. So I decided to post altered class with ability to start service with parameters:
在这篇文章中,我阅读了所有帖子和评论,但我仍然不知道当我要添加 Windows 服务时如何设置帐户类型和 StartType 方法。 此代码示例在我这边运行良好,只需添加一个服务自己的本地系统)但我准备安装程序时,由于客户系统,我必须考虑StartMode和用户帐户类型方法。< br>
ServiceBootFlag 枚举似乎提供 StartType,但 帐户类型 仍然是一个问题。
In this article I read all posts and comments Bu still I don't know How can I set Account Type and StartType method when I gonna add windows service. This code example works fine my side its just add a service own local system) But I prepare Setup program I must think StartMode and User Account Type method because of customers system.
there is every appearance that ServiceBootFlag enum provide StartType but Account Type still a problem.
使用以下代码使用 C# 安装 Windows 服务:
Use below code to install windows service using C#:
另一种方法可以是从命令提示符使用 SC.exe:
Another method could be using SC.exe from command prompt: