国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行

2020-01-24 03:07:32
字體:
供稿:網(wǎng)友

本實(shí)例主要實(shí)現(xiàn)下面三個(gè)基本功能

1、C#開發(fā)windows服務(wù)

2、禁止QQ等程序運(yùn)行

3、為windows服務(wù)創(chuàng)建自動(dòng)安裝程序

下面針對(duì)這三個(gè)基本功能進(jìn)行實(shí)現(xiàn)

一、C#開發(fā)windows服務(wù)

Windows服務(wù)在VS以前的版本中叫NT服務(wù),在VS.NET啟用了新的名稱。用C#創(chuàng)建windows服務(wù)不是一件困難的事,下頁針對(duì)服務(wù)創(chuàng)建、啟動(dòng)、停止做詳細(xì)介紹

1、首先在vs中添加一winform程序KillService

2、在解決方案添加新項(xiàng)中添加Windows服務(wù)

3、打開服務(wù)頁面,切換至代碼頁面有兩個(gè)方法如下:

復(fù)制代碼 代碼如下:

protected override void OnStart(string[] args)
{
   // TODO: 在此處添加代碼以啟動(dòng)服務(wù)。
}
protected override void OnStop()
{
// TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。
}

當(dāng)服務(wù)啟動(dòng)之后一般會(huì)要求每隔幾秒或者幾分鐘刷新一次數(shù)據(jù),所以要用到一個(gè)定時(shí)器,在定時(shí)器里邊進(jìn)行業(yè)務(wù)操作。windows服務(wù)不能直接在VS下進(jìn)行調(diào)試,所以可以選擇使用日志形式記錄服務(wù)的各種啟動(dòng)停止或者異常的狀態(tài)。具體實(shí)現(xiàn)代碼如下:

復(fù)制代碼 代碼如下:

partial class Service1 : ServiceBase
    {
        static System.Timers.Timer oTimer_Get = new System.Timers.Timer();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此處添加代碼以啟動(dòng)服務(wù)。
            AutoLog = false;
            FileLog.Success("服務(wù)已啟動(dòng)");
            oTimer_Get.Enabled = true;
            oTimer_Get.Interval = 10000;
            oTimer_Get.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
        }
        private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            FileLog.Success("開始發(fā)送");
            oTimer_Get.Enabled = false;
            try
            {
                //此處可進(jìn)行編寫詳細(xì)的業(yè)務(wù)操作
            }
            catch (Exception ex)
            {
                FileLog.Error(ex.Source + "。" + ex.Message);
            }
            oTimer_Get.Enabled = true;
            FileLog.Success("結(jié)束發(fā)送");
        }
        protected override void OnStop()
        {
            // TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。
            FileLog.Success("服務(wù)已停止");
            oTimer_Get.Enabled = false;
        }
    }

文件記錄類代碼

復(fù)制代碼 代碼如下:

/// <summary>
    /// 文件型日志記錄
    /// </summary>
    public static class FileLog
    {
        private static string sFilePath = System.Configuration.ConfigurationSettings.AppSettings["UserLog"];
        /// <summary>
        /// 錯(cuò)誤日志
        /// </summary>
        /// <param name="Message">錯(cuò)誤內(nèi)容</param>
        public static void Error(string Message)
        {
            try
            {
                if (!Directory.Exists(sFilePath))
                {
                    Directory.CreateDirectory(sFilePath);
                }
                string sFileName = sFilePath + "http://" + string.Format("{0}-Error.txt", DateTime.Now.ToString("yyyy-MM-dd"));
                string sContent = string.Format("{0}-- {1}/r/n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Message);
                FileStream fs = new FileStream(sFileName, FileMode.Append);
                Byte[] b = Encoding.Default.GetBytes(sContent);
                fs.Write(b, 0, b.Length);
                fs.Close();
            }
            catch { }
        }
        /// <summary>
        /// 正確日志
        /// </summary>
        /// <param name="Message">正確內(nèi)容</param>
        public static void Success(string Message)
        {
            try
            {
                if (!Directory.Exists(sFilePath))
                {
                    Directory.CreateDirectory(sFilePath);
                }
                string sFileName = sFilePath + "http://" + string.Format("{0}-Success.txt", DateTime.Now.ToString("yyyy-MM-dd"));
                string sContent = string.Format("{0}-- {1}/r/n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Message);
                FileStream fs = new FileStream(sFileName, FileMode.Append);
                Byte[] b = Encoding.Default.GetBytes(sContent);
                fs.Write(b, 0, b.Length);
                fs.Close();
            }
            catch { }
        }
    }

4、服務(wù)需要一個(gè)啟動(dòng)入口,打開program.cs文件在main函數(shù)里邊編寫入口代碼如下:

復(fù)制代碼 代碼如下:

static class Program
    {
        /// <summary>
        /// 應(yīng)用程序的主入口點(diǎn)。
        /// </summary>
        [STAThread]
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] {
              new Service1()
            };
            ServiceBase.Run(ServicesToRun);
        }
    }

到此windows服務(wù)編寫完成,但是現(xiàn)在該服務(wù)沒有什么的業(yè)務(wù)操作功能。接下來實(shí)現(xiàn)禁止本機(jī)QQ程序運(yùn)行功能

二、禁止QQ等程序運(yùn)行

只需獲取本機(jī)所有運(yùn)行的進(jìn)行,通過Process.kill()方法結(jié)束該進(jìn)程即可

復(fù)制代碼 代碼如下:

Process[] process = Process.GetProcesses();
                for (int i = 0; i < process.Length; i++)
                {
                    if (process[i].ProcessName == "QQ")
                    {
                        process[i].Kill();
                    }
                }

將該操作放至windows服務(wù)中的業(yè)務(wù)操作模塊即可。

三、為windows服務(wù)創(chuàng)建自動(dòng)安裝程序

上面實(shí)現(xiàn)了windows服務(wù)基本的業(yè)務(wù)操作功能,下面為該windows服務(wù)創(chuàng)建自動(dòng)安裝程序,

1、切換至service.cs設(shè)計(jì)頁面,右鍵選擇添加安裝程序

2、這時(shí)項(xiàng)目中就添加了一個(gè)新類 ProjectInstaller 和兩個(gè)安裝組件 ServiceProcessInstaller 和 ServiceInstaller,并且服務(wù)的屬性值被復(fù)制到組件。

3、若要確定如何啟動(dòng)服務(wù),請(qǐng)右鍵 ServiceInstaller1屬性并將 StartType 屬性設(shè)置為適當(dāng)?shù)闹怠?/P>

Manual      服務(wù)安裝后,必須手動(dòng)啟動(dòng)。Automatic    每次計(jì)算機(jī)重新啟動(dòng)時(shí),服務(wù)都會(huì)自動(dòng)啟動(dòng)。Disabled     服務(wù)無法啟動(dòng)。

4、將serviceProcessInstaller類的Account屬性改為 LocalSystem這樣,不論是以哪個(gè)用戶登錄的系統(tǒng),服務(wù)總會(huì)啟動(dòng)。

這些windows服務(wù)的安裝程序已經(jīng)完成。通過從生成菜單中選擇生成來生成項(xiàng)目。

注意   不要通過按 F5 鍵來運(yùn)行項(xiàng)目――不能以這種方式運(yùn)行服務(wù)項(xiàng)目。

5、創(chuàng)建啟動(dòng)和停止文件

安裝文件Install.bat實(shí)現(xiàn)如下:

cd %SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe KillService.exe

Net Start 自動(dòng)查殺服務(wù)
sc config 自動(dòng)查殺服務(wù) start= auto 

KillService.exe是你生成的可執(zhí)行文件的路徑

自動(dòng)查殺服務(wù)是windows服務(wù)的名稱

停止文件Uninstall.bat文件的實(shí)現(xiàn)如下:

%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe /u KillService.exe

KillService.exe是你生成的可執(zhí)行文件的路徑

PS:當(dāng)這兩個(gè)文件保存成功之后,執(zhí)行時(shí)一定要“以管理員身份運(yùn)行”,否則服務(wù)啟動(dòng)出錯(cuò)

如果服務(wù)安裝成功,可以在資源管理器中的服務(wù)查看到正在運(yùn)行的自動(dòng)查殺服務(wù)。

至此整個(gè)實(shí)例基本完成

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 牡丹江市| 崇仁县| 棋牌| 长海县| 苍山县| 曲松县| 土默特左旗| 东莞市| 同仁县| 汽车| 横峰县| 潞城市| 沿河| 义马市| 固阳县| 新巴尔虎右旗| 龙山县| 荥阳市| 深泽县| 文昌市| 富裕县| 三台县| 邯郸市| 合肥市| 九寨沟县| 姜堰市| 冕宁县| 内江市| 建水县| 西平县| 太湖县| 德州市| 马鞍山市| 汉源县| 都江堰市| 登封市| 攀枝花市| 洪泽县| 望奎县| 理塘县| 东乡族自治县|