Windows 服务

Windows服务是指操作系统可以启动运行的程序。

编写一个windows服务至少需要以下三个部分:

1.服务程序的主体

2.服务安装程序,把服务安装到系统中,完成一些写注册表的操作。

3.服务控制程序,用代码控制服务的开关启停。

 

我们来看看第一个部分:

做法为:

1.创建一个项目,类型为Windows服务。单击ok以后,系统给我们生成了一个模板。一个program.cs和一个Service1.cs。双击Service1.cs会出现一个外观与windows Forms应用程序类似的设计器。双击这个文件,会在右下角出现property属性编辑窗口。里面有Name属性,这个是实现这个Service的类的名字,还有一个ServiceName是写到注册表中的服务名称,使用这个名称可以控制服务。

Windows 服务_第1张图片

main中的代码为

<textarea cols="50" rows="15" name="code" class="c-sharp">static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); }</textarea>

service1.cs中的代码为

<textarea cols="50" rows="15" name="code" class="c-sharp">public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } }</textarea>

现在如果我们把Name属性改掉,那么上面中的Service1名字也会相应的改掉。但Service1.cs文件的名字不会随之改变,可以随意改。

在OnStart()方法里面是服务启动的时候运行的代码,这里我们可以放入任何我们想让其执行的代码。Main中的ServiceBase数组里面的元素是我们要跑的服务,可以加入多个。ServiceBase.Run()方法并不是启动服务,而是把SCM的引用提供给服务的入口点。让SCM可以控制服务。

2.我们安装这个服务。在这个项目里面add一个Installer Class,编辑它的代码。对于每一个你想要安装的服务编写 System.ServiceProcess.ServiceProcessInstaller ,System.ServiceProcess.ServiceInstaller,注意代码里面指定了要安装名字为QuoteService的服务。这个名字对应于上面服务中的ServiceName的属性值。

<textarea cols="50" rows="15" name="code" class="c-sharp">partial class Installer1 { /// &lt;summary&gt; /// Required designer variable. /// &lt;/summary&gt; private System.ComponentModel.IContainer components = null; private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; /// &lt;summary&gt; /// Clean up any resources being used. /// &lt;/summary&gt; /// &lt;param name=&quot;disposing&quot;&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt; protected override void Dispose(bool disposing) { if (disposing &amp;&amp; (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// &lt;/summary&gt; private void InitializeComponent() { this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; this.serviceInstaller1.ServiceName = &quot;QuoteService&quot;; this.Installers.AddRange( new System.Configuration.Install.Installer[]{ this.serviceProcessInstaller1,this.serviceInstaller1 } ); } #endregion }</textarea>

当build了这个项目后,我们就可以看见一个编译后的exe文件。到C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727中有一个InstallUtil.exe。在cmd中转到这个目录下运行【installutil 那个编译后的exe文件的完整目录】安装。安装的时候可能会弹出一个框叫你输入用户名和密码。如下操作

当弹出窗口(注:Set Service Login对话框)时,输入用来运行该服务的帐户的用户名和密码。用户名必须以domain/username的形式。(注:如果不是domain内帐 户,可以使用YourComputerName/administrator或./administrator的形式)

当完成后,你就会发现可以在服务列表中找到这个服务了。运行 【installutil /u 那个编译后的exe文件的完整目录】卸载。

3.编写控制程序

新建立一个控制台项目,随便起一个名字。在主函数中写如下的代码:

<textarea cols="50" rows="15" name="code" class="c-sharp">class Program { static void Main(string[] args) { ServiceController service = ServiceController.GetServices().First(x=&gt;x.ServiceName==&quot;QuoteService&quot;); service.Start(); } }</textarea>

找到这个服务,就可以调用ServiceController的各种方法对这个服务进行操作了。

 

你可能感兴趣的:(windows,service,Class,resources,installer,Components)