.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.查看服務效果






發佈了45 篇原創文章 · 獲贊 53 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章