为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨__羊羊 2019-10-24 11:19:09
我的VS解决方案中有3个项目。其中一个是Web应用程序,第二个是Windows服务,最后一个是我的Web应用程序的安装项目。我想要的是在安装项目中完成Web应用程序的安装,在我自定义动作中尝试安装Windows服务的前提下,前提是我那时具有程序集的位置。
查看完整描述

3 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

好的,这是真正为我工作的东西,它已经在具有不同操作系统(Vista,XP,Win2k,Win2003服务器)的多台计算机上进行了测试。


该代码已从此处获取,因此无论谁编写此代码都应归功于此。


将dll或源文件添加到项目后,请确保添加ServiceTools命名空间,然后您可以访问一些非常方便的功能,例如...


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


查看完整回答
反对 回复 2019-10-24
  • 3 回答
  • 0 关注
  • 621 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信