.NET Windows服务

1.新建Windows服务工程,选择的.NET环境为.net 4.0



2.双击Service查看代码



3.添加代码

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

namespace WService
{
    public partial class Service : ServiceBase
    {
        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //服务开启执行代码
            StartDoSomething();
        }

        protected override void OnStop()
        {
            //服务结束执行代码
        }

        protected override void OnPause()
        {
            //服务暂停执行代码
            base.OnPause();
        }
        protected override void OnContinue()
        {
            //服务恢复执行代码
            base.OnContinue();
        }
        protected override void OnShutdown()
        {
            //系统即将关闭执行代码
            base.OnShutdown();
        }
        private void StartDoSomething()
        {
            System.Timers.Timer timer = new System.Timers.Timer(10000); //间隔10秒
            timer.AutoReset = true;
            timer.Enabled = false;  //执行一次
            timer.Elapsed += new ElapsedEventHandler(WriteSomething);
            timer.Start();
        }
        private void WriteSomething(object source, System.Timers.ElapsedEventArgs e)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream("d:/WService.txt", FileMode.OpenOrCreate);
                string strText = @"//实例化一个文件流--->与写入文件相关联
               FileStream fs = newFileStream(sf.FileName, FileMode.Create);
               //实例化一个StreamWriter-->与fs相关联
               StreamWriter sw = newStreamWriter(fs);
               //开始写入
               sw.Write(this.textBox1.Text);
               //清空缓冲区
               sw.Flush();
               //关闭流
               sw.Close();
               fs.Close();";
                //获得字节数组
                byte[] data = new UTF8Encoding().GetBytes(strText);
                //开始写入
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
            catch
            {
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
    }
}


4.双击Service.cs文件



5.左侧Design视图设计的视图中空白处右键选择 添加安装程序(Add Installer)



6.选中serviceProcessInstaller1组件,查看属性,设置account为LocalSystem



7.选中serviceInstaller1组件,查看属性
设置ServiceName的值, 该值表示在系统服务中的名称<不能同计算机中其它服务名称相同>
设置StartType, 如果为Manual则手动启动,默认停止,如果为Automatic为自动启动
设置Description,添加服务描述



8.编译生成



9.打开命令提示符,以管理员的身份运行(不以管理员的身份运行将导致服务安装失败)

输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车进入InstallUtil.exe所在目录



10.安装服务(E:\Project\WService\WService\bin\Debug\WService.exe 为编译完成的服务的路径)

输入


回车



11.服务安装成功

查看

我的电脑---管理---服务和应用程序---服务

双击服务



12.选择标准,查找WSer



13.鼠标右键点击,选择“启动(S)”



14.查看服务效果






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值