2009-12-30 157 views
0

我有安装简单的Windows服务的问题,我的代码看起来是这样的:在Windows 7 .NET Windows服务的安装问题,2008

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.ServiceProcess; 
using System.Threading; 

namespace Tools.FileMonitorService 
{ 
    public class Service : System.ServiceProcess.ServiceBase 
    { 
     private System.ComponentModel.Container components = null; 

     public Service() 
     { 
      InitializeComponent(); 
     } 

     static void Main() 
     { 
      System.ServiceProcess.ServiceBase[] ServicesToRun; 
      ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() }; 
      System.ServiceProcess.ServiceBase.Run(ServicesToRun); 
     } 

     private void InitializeComponent() 
     { 
      components = new System.ComponentModel.Container(); 
      this.ServiceName = "Tools.FileMonitorService"; 
     } 

     protected override void Dispose(bool disposing) 
     { 
      if (disposing) 
      { 
       if (components != null) 
       { 
        components.Dispose(); 
       } 
      } 
      base.Dispose(disposing); 
     } 

     protected override void OnStart(string[] args) 
     { 
     while (true) 
     { 
     Debug.Write("Ping"); 
     Thread.Sleep(10000); 
       }    
     } 

     protected override void OnStop() 
     { 
      Debug.Write("Stopping"); 
     } 
    } 
} 

当我运行以下commnad:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i Tools.FileMonitorService.exe 

有没有错误,一切都很好,但是当我查看Windows服务列表时,我看不到我的服务。

我做错了什么?

我试着做它在Windows 7和Windows 2008服务器

非常感谢你!

+0

你可以发布InstallUtil.exe的输出吗? – Krunal 2009-12-30 12:44:19

回答

2

代码片段中没有ServiceInstaller的标志,也没有标记为[RunInstaller(true)]属性的安装方法。这将解释为什么它不起作用。

在MSDN Library文章ServiceInstaller class中有一个很好的例子。

+0

确实我没有ServiceInstaller类 谢谢 – 2009-12-30 13:06:51