欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#創(chuàng)建Windows服務的實現(xiàn)方法

 更新時間:2019年03月21日 08:18:44   作者:鍵盤演繹青春  
這篇文章主要介紹了C#創(chuàng)建Windows服務的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Microsoft Windows 服務能夠創(chuàng)建在它們自己的 Windows 會話中可長時間運行的可執(zhí)行應用程序。這些服務可以在計算機啟動時自動啟動,可以暫停和重新啟動而且不顯示任何用戶界面。這使服務非常適合在服務器上使用,或任何時候,為了不影響在同一臺計算機上工作的其他用戶,需要長時間運行功能時使用。還可以在不同于登錄用戶的特定用戶帳戶或默認計算機帳戶的安全上下文中運行服務。

一、創(chuàng)建Windows 服務

1.新建一個Windows 服務,并將項目名稱改為“WindowsServiceDemo”,如下圖所示:

2.在解決方案資源管理器內將Service1.cs改為MyService.cs后并點擊“查看代碼”圖標按鈕進入代碼編輯器界面,如下圖所示:

3.在代碼編輯器內如入以下代碼,如下所示:

using System;
using System.ServiceProcess;
using System.IO;

namespace WindowsServiceDemo
{
  public partial class MyService : ServiceBase
  {
    public MyService()
    {
      InitializeComponent();
    }

    string filePath = @"D:\MyServiceLog.txt";

    protected override void OnStart(string[] args)
    {
      using (FileStream stream = new FileStream(filePath, FileMode.Append))
      using (StreamWriter writer = new StreamWriter(stream))
      {
        writer.WriteLine($"{DateTime.Now},服務啟動!");
      }
    }

    protected override void OnStop()
    {
      using (FileStream stream = new FileStream(filePath, FileMode.Append))
      using (StreamWriter writer = new StreamWriter(stream))
      {
        writer.WriteLine($"{DateTime.Now},服務停止!");
      }
    }
  }
}

4.雙擊項目“WindowsServiceDemo”進入“MyService”設計界面,在空白位置右擊鼠標彈出上下文菜單,選中“添加安裝程序”,如下圖所示:

5.此時軟件會生成兩個組件,分別為“serviceInstaller1”及“serviceProcessInstaller1”,如下圖所示:

6.點擊“serviceInstaller1”,在“屬性”窗體將ServiceName改為MyService,Description改為我的服務,StartType保持為Manual,如下圖所示:

7.點擊“serviceProcessInstaller1”,在“屬性”窗體將Account改為LocalSystem(服務屬性系統(tǒng)級別),如下圖所示:

8.鼠標右鍵點擊項目“WindowsServiceDemo”,在彈出的上下文菜單中選擇“生成”按鈕,如下圖所示:

9.至此,Windows服務已經(jīng)創(chuàng)建完畢。

二、創(chuàng)建安裝、啟動、停止、卸載服務的Windows窗體

1.在同一個解決方案里新建一個Windows Form項目,并命名為WindowsServiceClient,如下圖所示:

2.將該項目設置為啟動項目,并在窗體內添加四個按鈕,分別為安裝服務、啟動服務、停止服務及卸載服務,如下圖所示:

3.按下F7進入代碼編輯界面,添加引用“System.ServiceProcess”及“System.Configuration.Install”,并輸入如下代碼:

using System;
using System.Collections;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;

namespace WindowsServiceClient
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    string serviceFilePath = $"{Application.StartupPath}\\WindowsServiceDemo.exe";
    string serviceName = "MyService";

    //事件:安裝服務
    private void button1_Click(object sender, EventArgs e)
    {
      if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
      this.InstallService(serviceFilePath);
    }

    //事件:啟動服務
    private void button2_Click(object sender, EventArgs e)
    {
      if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
    }

    //事件:停止服務
    private void button3_Click(object sender, EventArgs e)
    {
      if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
    }

    //事件:卸載服務
    private void button4_Click(object sender, EventArgs e)
    {
      if (this.IsServiceExisted(serviceName))
      {
        this.ServiceStop(serviceName);
        this.UninstallService(serviceFilePath);
      }
    }

    //判斷服務是否存在
    private bool IsServiceExisted(string serviceName)
    {
      ServiceController[] services = ServiceController.GetServices();
      foreach (ServiceController sc in services)
      {
        if (sc.ServiceName.ToLower() == serviceName.ToLower())
        {
          return true;
        }
      }
      return false;
    }

    //安裝服務
    private void InstallService(string serviceFilePath)
    {
      using (AssemblyInstaller installer = new AssemblyInstaller())
      {
        installer.UseNewContext = true;
        installer.Path = serviceFilePath;
        IDictionary savedState = new Hashtable();
        installer.Install(savedState);
        installer.Commit(savedState);
      }
    }

    //卸載服務
    private void UninstallService(string serviceFilePath)
    {
      using (AssemblyInstaller installer = new AssemblyInstaller())
      {
        installer.UseNewContext = true;
        installer.Path = serviceFilePath;
        installer.Uninstall(null);
      }
    }
    //啟動服務
    private void ServiceStart(string serviceName)
    {
      using (ServiceController control = new ServiceController(serviceName))
      {
        if (control.Status == ServiceControllerStatus.Stopped)
        {
          control.Start();
        }
      }
    }

    //停止服務
    private void ServiceStop(string serviceName)
    {
      using (ServiceController control = new ServiceController(serviceName))
      {
        if (control.Status == ServiceControllerStatus.Running)
        {
          control.Stop();
        }
      }
    } 
  }
}

4.為了后續(xù)調試服務及安裝卸載服務的需要,將已生成的WindowsServiceDemo.exe引用到本W(wǎng)indows窗體,如下圖所示:

5.由于需要安裝服務,故需要使用UAC中Administrator的權限,鼠標右擊項目“WindowsServiceClient”,在彈出的上下文菜單中選擇“添加”->“新建項”,在彈出的選擇窗體中選擇“應用程序清單文件”并單擊確定,如下圖所示:

6.打開該文件,并將<requestedExecutionLevel level="asInvoker" uiAccess="false" />改為<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,如下圖所示:

7.IDE啟動后,將會彈出如下所示的窗體(有的系統(tǒng)因UAC配置有可能不顯示),需要用管理員權限打開:

8.重新打開后,在IDE運行WindowsServiceClient項目;

9.使用WIN+R的方式打開運行窗體,并在窗體內輸入services.msc后打開服務,如下圖所示:

10.運行程序"Form1"點擊窗體內的“安裝服務”按鈕,將會在服務中出現(xiàn)MyService,如下圖所示:

11.點擊“運行服務”按鈕,將啟動并運行服務,如下所示:

12.點擊“停止服務”按鈕,將會停止運行服務,如下圖所示:

13.點擊“卸載服務”按鈕,將會從服務中刪除MyService服務。

14.以上啟動及停止服務將會寫入D:\MyServiceLog.txt,內容如下所示:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • treeview遞歸綁定的兩種方法

    treeview遞歸綁定的兩種方法

    這篇文章主要介紹了treeview遞歸綁定的兩種方法,需要的朋友可以參考下
    2014-04-04
  • WPF中NameScope的查找規(guī)則詳解

    WPF中NameScope的查找規(guī)則詳解

    這篇文章主要給大家介紹了關于WPF中NameScope的查找規(guī)則的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-10-10
  • WinForm自定義函數(shù)FindControl實現(xiàn)按名稱查找控件

    WinForm自定義函數(shù)FindControl實現(xiàn)按名稱查找控件

    這篇文章主要介紹了WinForm自定義函數(shù)FindControl實現(xiàn)按名稱查找控件,需要的朋友可以參考下
    2014-08-08
  • C# Winform實現(xiàn)導入和導出Excel文件

    C# Winform實現(xiàn)導入和導出Excel文件

    這篇文章主要為大家詳細介紹了C# Winform實現(xiàn)導入和導出Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • C#?Web實現(xiàn)文件上傳的示例詳解

    C#?Web實現(xiàn)文件上傳的示例詳解

    這篇文章主要為大家詳細介紹了C#?Web實現(xiàn)文件上傳的相關知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • winform用datagridview制作課程表實例

    winform用datagridview制作課程表實例

    這篇文章主要介紹了winform用datagridview制作課程表的方法,實例分析了WinForm實現(xiàn)課程表的結構、數(shù)據(jù)庫及調用技巧,需要的朋友可以參考下
    2015-01-01
  • c# Base關鍵字的使用

    c# Base關鍵字的使用

    c# Base關鍵字的使用示例代碼,大家可以參考下用法。
    2009-07-07
  • C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值(示例代碼)

    C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值(示例代碼)

    這篇文章主要介紹了C#使用表達式樹(LambdaExpression)動態(tài)更新類的屬性值,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • WPF利用RPC調用其他進程的方法詳解

    WPF利用RPC調用其他進程的方法詳解

    這篇文章主要給大家介紹了關于WPF利用RPC調用其他進程的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-05-05
  • 詳細分析c# 運算符重載

    詳細分析c# 運算符重載

    這篇文章主要介紹了c# 運算符重載的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07

最新評論