LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#使用ServiceController控制Windows服務的停止、啟動、重啟

admin
2021年6月15日 10:30 本文熱度 5019

C#使用ServiceController控制類Windows服務,添加首次使用前引文:System.ServiceProcess,空間中引用:using System.ServiceProcess

以下示例獲取本機已安裝的Windows服務和應用,然后查找某一應用活服務是否已經(jīng)安裝。

代碼:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.ServiceProcess;

namespace 判斷機器中是否安裝了某項服務或者應用

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        ServiceController[] Services = ServiceController.GetServices();

        private bool ExistSth()

        {

            bool exist = false;

            for (int i = 0; i < Services.Length; i++)

            {

                if (Services[i].DisplayName.ToString() == textBox1.Text.Trim())

                    exist = true; 

            }

            return exist;

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (ExistSth())

                MessageBox.Show("已安裝");

            else

                MessageBox.Show("未安裝");

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < Services.Length; i++)

                listBox1.Items.Add(Services[i].DisplayName.ToString());

        }

    }

}


如果某一服務名為ServicesName, 編寫開始服務、停止服務、重新啟動服務的代碼如下:

private ServiceController _controller;

private void StopService()

{

    this._controller = new ServiceController("ServicesName");

    this._controller.Stop();

    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);

    this._controller.Close();

}

private void StartService()

{

    this._controller = new ServiceController("ServicesName");

    this._controller.Start();

    this._controller.WaitForStatus(ServiceControllerStatus.Running);

    this._controller.Close();

}

private void ResetService()

{

    this._controller = new ServiceController("ServicesName");

    this._controller.Stop();

    this._controller.WaitForStatus(ServiceControllerStatus.Stopped);

    this._controller.Start();

    this._controller.WaitForStatus(ServiceControllerStatus.Running);

    this._controller.Close();

}


該文章在 2021/6/16 8:50:24 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務費用、相關報表等業(yè)務管理,結合碼頭的業(yè)務特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲精品亚洲人成在线 | 亚洲人成在线播放 | 一a在线视频插进去了 | 亚洲精品最新国产自在自 | 日本高清中文字幕专区 | 无遮挡国产高潮视频免费观看 |