如何在C#编程方式安装Windows服务?方式、如何在、Windows

2023-09-02 10:19:48 作者:离夏天最远的地方

我有3个项目在我与解决方案。 1是一个Web应用程序,另一种是窗口服务,最后一个一个安装项目我的web应用程序。

I have 3 projects in my VS solution. 1 is a web app, the other is a windows service and the last one a setup project for my web app.

我要的是在我安装项目中的Web应用程序的安装结束,在我的自定义操作,试图安装我的Windows服务,因为我那时有装配的位置。

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.

感谢提前的所有帮助。

推荐答案

好吧,这里是真正的工作对我来说,它已经过测试,在多台机器上使用不同的操作系统(Vista中,XP,在Win2k,Win2003的服务器)

Ok, here is what REALLY worked for me, it has been tested on multiple machines with different OS ( Vista, XP, Win2k, Win2003 server )

在code摘自here所以完全归功于谁写这片code。

The code has been taken from here so full credit goes to whoever wrote this piece of code.

您可以在这里找到源文件或者你可能也抢的这个的DLL,我prepared你

You can find the source file here or you may as well grab this dll that i prepared for you.

在你的DLL或源文件添加到您的项目一定要添加ServiceTools命名空间,然后你可以使用一些非常方便的功能,如...

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

//Installs and starts the service
ServiceInstaller.InstallAndStart("MyServiceName", "MyServiceDisplayName", "C:PathToServiceFile.exe");

//Removes the service
ServiceInstaller.Uninstall("MyServiceName");

//Checks the status of the service
ServiceInstaller.GetServiceStatus("MyServiceName");

//Starts the service
ServiceInstaller.StartService("MyServiceName");

//Stops the service
ServiceInstaller.StopService("MyServiceName");

//Check if service is installed
ServiceInstaller.ServiceIsInstalled("MyServiceName");

我希望这有助于。

I hope this helps.

-Konstantinos

-Konstantinos