windowsService 程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsServiceTest
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Console.WriteLine("ss");
            WriteLog("service启动了。");
        }

        protected override void OnStop()
        {
            WriteLog("service停止了。");
        }

        public void WriteLog(string str)
        {
            using (StreamWriter sw = File.AppendText(@"d:\service.txt"))
            {
                sw.WriteLine(str);
                sw.Flush();
            }
        }  
    }
}

六:安装卸载服务

选择 VS组件 “Visual Studio命令提示(2010)” 工具,并以“管理员身份运行"(win7、win8系统下)。

注意:这里必须选择“以管理员身份运行”,否则会报错。

  

从命令行运行 Installutil.exe 目录  命令,以项目中的已编译可执行文件所在的目录作为参数,安装服务:

1. 方法 1

因为Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目录下,需要通过cmd命令 "cd" 切换目录。

从命令行运行 Installutil.exe /u 目录   命令来卸载服务:

安装服务: installutil.exe E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

卸载服务: installutil.exe /u E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

1. 方法 2

找到 Installutil.exe 文件,并把它复制到 E:\XTestDemo\X_15_WindowsService\bin\Debug\ 目录

现在 Installutil.exe 程序在 E:\XTestDemo\X_15_WindowsService\bin\Debug\ 目录下,需要通过cmd命令 "cd" 切换目录。

安装服务: installutil.exe X_15_WindowsService.exe

卸载服务: installutil.exe X_15_WindowsService.exe

七:查看服务状态

在“计算机管理”中,服务 下可以看到刚刚安装的Service服务(cmd命令:services.msc---本地服务设置):

默认是停止状态。右击,选择“启动”,即可开启服务。

通过“属性”,可以查看到更详细的信息。

 

原文地址:https://www.cnblogs.com/james641/p/6307027.html

posted on 2018-03-26 21:08  sxjljj  阅读(172)  评论(0编辑  收藏  举报