码迷,mamicode.com
首页 > Windows程序 > 详细

创建Windows服务简单流程

时间:2014-08-29 01:19:16      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   os   io   ar   for   文件   

1.首先打开VS2010(或者其他版本),创建Windows服务项目

 

 bubuko.com,布布扣

 

 

2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释:

 bubuko.com,布布扣

注意选择的是系统时间,不是winform中的时间。

using System;

using System.IO;

usingSystem.ServiceProcess;

using System.Text;

usingSystem.Timers;

namespaceTestService

{

   public partial class Service1 : ServiceBase

   {

       public Service1()

       {

           InitializeComponent();

       }

       protected override voidOnStart(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 = newSystem.Timers.Timer(10000); //间隔10秒

           timer.AutoReset = true;开机启动

           timer.Enabled = false;  //执行一次,true时没10S执行一次。

           timer.Elapsed += new ElapsedEventHandler(WriteSomething);

           timer.Start();

       }

       private void WriteSomething(objectsource, System.Timers.ElapsedEventArgs e)

       {

           FileStream fs = null;

           try

           {

               fs = new FileStream("d:/1.txt", FileMode.OpenOrCreate);

               string strText = @"//实例化一个文件流--->与写入文件相关联

               FileStream fs = new FileStream(sf.FileName, FileMode.Create);

               //实例化一个StreamWriter-->与fs相关联

               StreamWriter sw = new StreamWriter(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();

               }

           }

       }

   }

}

3.然后切换到设计视图,右键点击下图中圈选的“添加安装程序

 

 bubuko.com,布布扣

4.选中下图第一个控件,点击F4,右边切换到属性视图;更改属性视图中的Account属性为LocalService(本地服务)

 bubuko.com,布布扣

5.选中上面第二个控件,点击F4,右边切换到属性视图。更改ServiceName为你自己喜欢的服务名称,记住不要和系统的冲突了,StartType默认为手动,你可以更改为自动

(Automatic)或禁用(Disabled)

 

6.编译项目,然后win+R输入cmd进入命令窗口。去对应.net版本下的目录中找到InstallUtil.exe,我项目采用的是 .net 4.0,故路径为C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319

7.InstallUtil.exe对应.net版本目录图,如下

 bubuko.com,布布扣

8.然后win+R输入cmd进入命令窗口。

bubuko.com,布布扣

9.

方法删除服务:直接进行注册表编辑            打开注册表编辑器,找到下面的键值:
            HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services        一般服务会以相同的名字在这里显示一个主健,直接删除相关的键值便可。

 

 

创建Windows服务简单流程

标签:winform   style   blog   http   os   io   ar   for   文件   

原文地址:http://www.cnblogs.com/SamllBaby/p/3943953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!