[轉]C#創建服務及使用程序自動安裝服務,.NET創建一個即是可執行程序又是Windows服務的exe


寫在前面

原文地址:C#創建服務及使用程序自動安裝服務,.NET創建一個即是可執行程序又是Windows服務的exe

這篇文章躺在我的收藏夾中有很長一段時間了,今天閑着沒事,就自己動手實踐了一下。感覺作者還是蠻給力的。這里動手實踐,把它變為自己的東西。多謝作者。

操作步驟

最近在弄的項目,一直在考慮是否弄windows服務來定時拉取數據。然后就按照這個思路,想了一下,發現跟之前看的一篇文章需求很是類似。就弄了個demo,把那種方式吸收下來。

首先,創建一個控制台應用程序。

然后,添加一個windows服務類。

項目

然后打開SericeHost.cs,右鍵查看代碼

代碼如下:

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.Threading.Tasks;

namespace Wolfy.SelfService
{
    partial class ServiceHost : ServiceBase
    {
        public ServiceHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            System.IO.File.AppendAllText("D:\\log.txt", "Service Is Started……" + DateTime.Now.ToString());
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            System.IO.File.AppendAllText("D:\\log.txt", "Service Is Stopped……" + DateTime.Now.ToString());
        }
    }
}

修改入口函數Main方法,根據agrs參數,來識別是安裝服務,還是應用程序。如果是服務則執行安裝服務的步驟,否則則運行程序。

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

namespace Wolfy.SelfService
{
    class Program
    {
        static void Main(string[] args)
        {
            //如果傳遞了"s"參數就啟動服務
            if (args.Length > 0 && args[0] == "s")
            {
                //啟動服務的代碼,可以從其它地方拷貝
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new ServiceHost(),
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                Console.WriteLine("這是Windows應用程序");
                Console.WriteLine("請選擇,[1]安裝服務 [2]卸載服務 [3]退出");
                var rs = int.Parse(Console.ReadLine());
                switch (rs)
                {
                    case 1:
                        //取當前可執行文件路徑,加上"s"參數,證明是從windows服務啟動該程序
                        var path = Process.GetCurrentProcess().MainModule.FileName + " s";
                        Process.Start("sc", "create myserver binpath= \"" + path + "\" displayName= 我的服務 start= auto");
                        Console.WriteLine("安裝成功");
                        Console.Read();
                        break;
                    case 2:
                        Process.Start("sc", "delete myserver");
                        Console.WriteLine("卸載成功");
                        Console.Read();
                        break;
                    case 3: break;
                }
            }

        }
    }
}

測試
以管理員身份運行Wolfy.SelfService.exe可執行程序。

這里需要注意,要以管理員身份運行,畢竟是安裝windows服務,否則權限不夠,造成安裝失敗。

輸入1

然后win+R,輸入services.msv

服務安裝成功,下面我們啟動服務。

查看下log.txt

說明服務安裝成功,並啟動成功。

停止服務,然后卸載服務。

Service Is Started……2015/5/8 21:14:11Service Is Stopped……2015/5/8 21:16:21

說明服務停止成功,然后進行卸載。

同樣,卸載也要以管理員身份運行。

然后在服務管理中,發現“我的服務”已經卸載。

總結

這里按照作者的思路,進行了實踐,這個思路確實不錯,實踐一下,也算吸收了。多謝。這篇文章也可以從我的收藏夾中移除了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM