除非在这个windows程序里面设置好,否则一般windows程序是不支持的!

解决方案 »

  1.   

    可以做到,但你必须在你的程序中添加一个Service组件。并且修改你的Main方法。
      

  2.   

    在Windows服务工程中,建一个安装类让它继承自System.Configuration.Install.Installer类,然后在InitializeComponent方法中声明ServiceProcessInstaller和ServiceInstaller类,然后将这两个对象加到安装类Installers属性集合中.代码如下:
    [RunInstaller(true)]       public class Installer1 : System.Configuration.Install.Installer       {              /// <summary>              /// 必需的设计器变量。              /// </summary>              private System.ComponentModel.Container components = null;              private System.ServiceProcess.ServiceProcessInstaller spInstaller;              private System.ServiceProcess.ServiceInstaller sInstaller;               public Installer1()              {                     // 该调用是设计器所必需的。                     InitializeComponent();                     // TODO: 在 InitComponent 调用后添加任何初始化              }               #region Component Designer generated code              /// <summary>              /// 设计器支持所需的方法 - 不要使用代码编辑器修改              /// 此方法的内容。              /// </summary>             private void InitializeComponent()              {                     components = new System.ComponentModel.Container();                      // 创建ServiceProcessInstaller对象和ServiceInstaller对象                     this.spInstaller =               new System.ServiceProcess.ServiceProcessInstaller();                     this.sInstaller = new System.ServiceProcess.ServiceInstaller();                      // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息                     this.spInstaller.Account =               System.ServiceProcess.ServiceAccount.LocalSystem;                     this.spInstaller.Username = null;                     this.spInstaller.Password = null;                     // 设定服务名称                     this.sInstaller.ServiceName = "FileMonitorService";                     // 设定服务的启动方式                     this.sInstaller.StartType =               System.ServiceProcess.ServiceStartMode.Automatic;                      this.Installers.AddRange(              new System.Configuration.Install.Installer[]                 {this.spInstaller, this.sInstaller });              }              #endregion       }
    注意:类头前面的[RunInstaller(true)]必须得写,这是正确执行安装的前提.
    打开.Net的SDK命令提示,输入installutil FileMonitorService.exe,这里的FileMonitorService.exe是相对路径名.输入命令之后就可以安装完成
      

  3.   

    另有一种办法可以不写代码就可以安装windows服务
    在注册表中找到:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services
    新建一个文件夹,在这个文件夹建立几个项目:
    1.Description这是windows服务的描述
    2.DisplayName这是windows服务的显示名
    3.ErrorControl是否提供错误控制
    4.ImagePath这是windows服务的绝对路径
    5.Start这是windows服务的启动类型
    6.Type这是windows服务的类型
    7.ObjectName是windows服务的帐户类型.
    注意:文件夹的名称就是windows服务真实名称.
    然后,重启一下机子就安装成功.