創建windows定時,系統服務,卸載和安裝詳細步驟

1 新建 windows 服務 項目



2 添加自定義設置文件


3 配置基本參數,

在程序中可以如下使用 這裏定義的常量 Timing_WX_L.Settings1.Default.span,

4 設置安裝參數  可參考 http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html

  1. 一、打開Service1.cs視圖界面

  2. 在視圖內右鍵-->添加安裝程序

  3. 二、項目中添加了ProjectInstaller.cs文件,該文件中視圖自動會添加倆個組件

    serviceProcessInstaller1

    serviceInstaller1


    三、選中serviceProcessInstaller1組件,查看屬性,設置account爲LocalSystem
  4. 四、在視圖內右鍵-->添加安裝程序

  5. 五、項目中添加了ProjectInstaller.cs文件,該文件中視圖自動會添加倆個組件

    serviceProcessInstaller1

    serviceInstaller1


  6. 六、選中serviceProcessInstaller1組件,查看屬性,設置account爲LocalSystem

  7. 七、選中serviceInstaller1組件,查看屬性

    設置ServiceName的值, 該值表示在系統服務中的名稱

    設置StartType, 如果爲Manual則手動啓動,默認停止,如果爲Automatic爲自動啓動

    設置Description,添加服務描述

  8. 八、重新生成項目



---------------------------------------------------------------以上是準備工作----------------------------------------------------------------------


5 在程序運行主程序裏,填寫如下程序

 partial class Wx_pay_L : ServiceBase
    {
        Timer _timer = new Timer();
        string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "logWX.txt";// "D:\\ws.txt";
        static int i = 0;
        static int span = 0;
        public Wx_pay_L()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
          
            //時間間隔 
            span = Convert.ToInt32(Timing_WX_L.Settings1.Default.span);

            _timer.AutoReset = true;
            _timer.Interval = 1000 * span;// 默認*1000是1秒
            _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            try
            {
                TextWriter sw = new StreamWriter(filePath, true);
                sw.WriteLine("------" + System.DateTime.Now.ToString());
                sw.WriteLine("服務啓動。");
                sw.Flush();
                sw.Close();
            }
            catch
            { }
            _timer.Start();
        }
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {

            try
            {
                string uri = "http://localhost:802/api/pay/everyGetAllData";
                WebClient wc = new WebClient();
                string r = wc.DownloadString(uri);

            }
            catch (Exception ex)
            {
                try
                {
                    TextWriter sw = new StreamWriter(filePath, true);
                    sw.WriteLine("------" + System.DateTime.Now.ToString());
                    sw.WriteLine(ex.Message);
                    sw.Flush();
                    sw.Close();
                }
                catch
                { }
            }
        }
        protected override void OnStop()
        {
            try
            {
                TextWriter sw = new StreamWriter(filePath, true);
                sw.WriteLine("------" + System.DateTime.Now.ToString());
                sw.WriteLine("服務停止。");
                sw.Flush();
                sw.Close();
            }
            catch
            { }
        }

        //private static string GetAppConfig(string strKey)   這裏是直接引用 webconfig
        //{
        //    foreach (string key in ConfigurationManager.AppSettings)
        //    {
        //        if (key == strKey)
        //        {
        //            return ConfigurationManager.AppSettings[strKey];
        //        }
        //    }
        //    return null;
        //}  
    }

6 生成之後的bin目錄裏就有exe文件,之後運行如下步驟安裝和卸載其爲系統服務

         一、  點擊 開始,運行中輸入cmd,獲取命令提示符

           win7需要已管理員的身份啓動,否則無法安裝

        二、輸入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回車
切換當前目錄,此處需要注意的是,在C:\Windows\Microsoft.NET\Framework目錄下有很多類似版本,具體去哪個目錄要看項目的運行環境,例 如果是.net framework2.0則需要輸入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

       三、輸入 InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 回車
說明:E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe表示項目生成的exe文件位置

     四、打開服務,就可以看到已經安裝的服務了;

      卸載很簡單,打開cmd, 直接輸入 sc delete WinServiceTest便可。還有問題可以參考文件下面的參考網址


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