打包——VS2008c# windows服务程序

windows 服务程序 打包、安装制作全过程。以visual studio 2008开发环境,c#语言为例。
步骤如下,

1、新建项目——Windows服务,新建一个"Windows服务"项目。

首先在视图设计器——属性——添加安装程序,设置serviceInstaller1对应Description、DisplayName、ServiceName三个属性,对应值为"我的第一个Windows服务程序","MyWindowsTest","MyWindowsTest".设置后自动生成如下代码。

如图所示:

view plaincopy to clipboardprint?
private void InitializeComponent()  
{  
    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();  
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();  
    //   
    // serviceProcessInstaller1  
    //   
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;  
    this.serviceProcessInstaller1.Password = null;  
    this.serviceProcessInstaller1.Username = null;  
    //   
    // serviceInstaller1  
    //   
    this.serviceInstaller1.DisplayName = "MyWindowsTest";  
    this.serviceInstaller1.ServiceName = "MyWindowsTest";  
    this.serviceInstaller1.Description = "我的第一个Windows服务程序";  
    //   
    // ProjectInstaller  
    //   
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {  
    this.serviceProcessInstaller1,  
    this.serviceInstaller1});  
 

        private void InitializeComponent()
        {
            this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
            //
            // serviceProcessInstaller1
            //
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            //
            // serviceInstaller1
            //
            this.serviceInstaller1.DisplayName = "MyWindowsTest";
            this.serviceInstaller1.ServiceName = "MyWindowsTest";
            this.serviceInstaller1.Description = "我的第一个Windows服务程序";
            //
            // ProjectInstaller
            //
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1});

        }

然后在设计视图中代码编辑器窗口中,单击 ServiceProcessInstaller 1 ,设置其属性窗格中Account属性设置为LocalSystem(目的是安装时允许所有登陆帐户)

2、在当前解决方案中新增一个安装工程项目
单击 安装和部署项目 下 项目类型 ,然后单击 模板 下的 安装项目
在解决方案资源管理器右键单击 ServiceSetup ,指向 添加 ,然后单击 项目输出
在 添加项目输出组 对话框的在 项目 框中单击 {前面的服务名称}
单击 主输出 ,然后单击 确定

在解决方案资源管理器右键单击 ServiceSetup ,指向 视图 ,然后单击 自定义操作
右键单击 自定义操作 ,然后单击 添加自定义操作
单击 应用程序文件夹 ,然后单击 确定
单击 主输出来自 {前面的服务名称} ,然后单击 确定

3、编译服务工程和安装工程

 运行:

手动安装服务方法:

如何:安装和卸载服务
与在 Visual Studio 中创建的大多数项目不同,Windows 服务项目不能通过按 F5 从开发环境直接运行。这是因为必须安装项目中的服务后,项目才能运行。

通过使用名为 InstallUtil.exe 的命令行实用程序,您可以迅速安装服务应用程序。您还可以创建包含项目输出的安装项目,并使用它创建一个可运行与项目关联的安装程序并安装服务的自定义操作。有关示例,请参见 演练:在组件设计器中创建 Windows 服务应用程序 。有关安装项目的更多信息,请参见 安装项目。有关自定义操作的更多信息,请参见 演练:创建自定义操作。

手动安装服务
访问项目中的已编译可执行文件所在的目录。

以项目的输出作为参数,从命令行运行 InstallUtil.exe。在命令行中输入下列代码:

复制代码
installutil yourproject.exe手动卸载服务
以项目的输出作为参数,从命令行运行 InstallUtil.exe。在命令行中输入下列代码:

复制代码
installutil /u yourproject.exe提示 
可以启动“服务器资源管理器”,然后验证服务是已安装还是已卸载。

原文地址:https://www.cnblogs.com/liuzhixian/p/1874883.html