如何以编程方式安装Windows Service
是否可以以编程方式运行Windows服务窗体Windows窗体应用程序?
我的意思是..在一个解决方案中,我有两个项目:服务和简单的Windows应用程序.
在应用程序中,我有三个按钮-开始",安装"和停止".我应该怎么办?
我必须从Windows服务生成exe文件吗?而且比以某种方式从应用程序运行它?我需要在解决方案中创建Windows Installer吗?
感谢您的帮助
我想构建一个客户端将以传统方式安装的程序-Simply Windows Forms应用程序.一个安装文件. And ..在此应用程序中将可以选择启动或停止Windows服务.
在Visual Studio中,我有一个想法,有两个项目-我想要一个安装文件.
Is it possible to run windows service form windows form application programmatically?
I mean.. In one solution, I have two projects: Service, and simple windows app.
In app i have three buttons - "Start", "Install" and "Stop". What should I do?
I must generate exe file from Windows Service? And than somehow run it from app? I need to create Windows Installer in my solution?
Thanks for your help
edit:
I would like to build a program that will be installed in the traditional manner by the client - Simply Windows Forms app. One setup file. And.. in this application will be option to start or stop a windows service.
In Visual Studio I''ve got one soultion, with two projects - I want one setup file.
您可以在一个可执行文件中全部完成.要传递安装参数,您将需要派生类System.ServiceProcess.ServiceProcessInstaller
和System.ServiceProcess.ServiceInstaller
.您只需要实现类的构造函数.
安装和卸载将由System.Configuration.Install.AssemblyInstaller
完成.此类应使用实现基于ServiceProcessInstaller
和ServiceInstaller
的类的程序集;该实现将在您的程序集中自动找到(如果实现中存在错误,则将导致难以发现的故障).不过还算不错.
最后,启动/停止等由您创建的服务代码处理,该服务代码派生出类System.ServiceProcess.ServiceBase
.触发此操作由服务控制器完成.要以编程方式执行此操作,应使用类System.ServiceProcess.ServiceController
.
您需要对MSDN进行很好的阅读,以了解我在上面列出的类周围的主题,代码示例对此进行了很好的说明.这些信息很容易找到,很难理解工作流程以及在哪个过程中会发生什么. (Windows Service进程始终是一个单独的进程;它需要另一个进程来运行安装和Service Controller的一部分.)您可以像食谱一样使用所有这些程序,但是我建议您完全理解它;在这种情况下,您不会有任何问题.
您甚至可以像我一样尝试在一个应用程序中实现所有功能,但仍然需要至少在两个不同的进程中运行它:一个可以在Windows Service模式下工作,另一个必须是交互模式,可以是其他任何模式,例如UI,控制台或不可见的批处理模式应用程序.
重要!您的代码可以在运行时测试System.Environment.UserInteractive
,以计算当前正在运行的代码是否以Windows Service运行.—SA
You can do it all in one executable. To pass installation parameters, you will need to derive classesSystem.ServiceProcess.ServiceProcessInstaller
andSystem.ServiceProcess.ServiceInstaller
. You only need to implement constructors of your classes.
Installation and uninstallation will be done bySystem.Configuration.Install.AssemblyInstaller
. This class should use the assembly where the classes based onServiceProcessInstaller
andServiceInstaller
are implemented; the implementation will be found in your assembly automatically (which would create a hard-to-detect failure if you have a bug in the implementation). Not too bad though.
Finally, start/stop, etc. are handled by your service code which you create deriving the classSystem.ServiceProcess.ServiceBase
. Triggering this actions is done by Service Controller. To do it programmatically you should use the classSystem.ServiceProcess.ServiceController
.
You need to have a good reading of MSDN on the topics around the classes I listed above, which is well illustrated by code samples. This information is easy to find, a bit harder to understand the work flow and what happens in which process. (A Windows Service process is always a separate one; it takes another process to run installation and Service Controller part.) You can use all this like a cookbook, but I suggest you thoroughly understand it; in this case you won''t have any problems.
You can even try to implement it all in one application as I did, but you still need to run it in at least two different processes: one would work in the Windows Service mode, another is must be interactive mode, which can be anything else, such as UI, Console or just invisible batch-mode application.
Important! You code can testSystem.Environment.UserInteractive
during run time to calculate is currently running code is run as Windows Service or not.—SA
是的,您可以做到.
此处:
启动,停止和重新启动Windows服务[C#] [ C#中的Windows服务:从另一个应用程序控制您的服务(第6部分) [发送自动电子邮件警报的简单Windows服务 [ Windows服务应用程序简介 [
Yes, you can do it.
Here:
Start, Stop and Restart Windows Service [C#][^]
Windows Services in C#: Controlling Your Service from Another Application (part 6)[^]
Try!
UPDATE:
Great articles, but i need to install first my service. That articles show me only how to start and stop service.
Come on man, make some effort. Sample:
Simple Windows Service which sends auto Email alerts[^]
MSDN: Details about Windows Service:
Introduction to Windows Service Applications[^]
I hope now you don''t say ''Now, I made Windows service, I know how to use it from a C# app but how to install it?''