C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行
本實(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è)方法如下:
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)代碼如下:
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;
}
}
文件記錄類代碼
/// <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 + "\\" + 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 + "\\" + 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ù)里邊編寫入口代碼如下:
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)程即可
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í)例基本完成
- C#基于Windows服務(wù)的聊天程序(1)
- 使用C#創(chuàng)建Windows服務(wù)的實(shí)例代碼
- C#使用windows服務(wù)發(fā)送郵件
- C#添加Windows服務(wù) 定時(shí)任務(wù)
- C#版Windows服務(wù)安裝卸載小工具
- C#通過創(chuàng)建Windows服務(wù)啟動(dòng)程序的方法詳解
- C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 基于C#實(shí)現(xiàn)Windows服務(wù)狀態(tài)啟動(dòng)和停止服務(wù)的方法
- c#使用windows服務(wù)更新站點(diǎn)地圖的詳細(xì)示例
- c#創(chuàng)建windows服務(wù)入門教程實(shí)例
- c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
- C#編寫Windows服務(wù)實(shí)例代碼
- C#啟動(dòng)和停止windows服務(wù)的實(shí)例代碼
- C#啟動(dòng)windows服務(wù)方法的相關(guān)問題分析
- c#開發(fā)的程序安裝時(shí)動(dòng)態(tài)指定windows服務(wù)名稱
- C#編寫Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
相關(guān)文章
C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法詳解
往數(shù)據(jù)庫批量寫入數(shù)據(jù),這個(gè)功能使用頻率相對(duì)還是比較高的,特別是在做一些導(dǎo)入等功能的時(shí)候。本文為大家介紹了C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法,需要的可以參考一下2022-11-11
C#實(shí)現(xiàn)數(shù)據(jù)包加密與解密實(shí)例詳解
這篇文章主要介紹了C#實(shí)現(xiàn)數(shù)據(jù)包加密與解密的方法,是一項(xiàng)很實(shí)用的技能,需要的朋友可以參考下2014-07-07
C# 定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問題解決
這篇文章主要介紹了C# 定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
將數(shù)組中指定數(shù)量的元素移動(dòng)數(shù)組后面的實(shí)現(xiàn)代碼
本篇文章是對(duì)將數(shù)組中指定數(shù)量的元素移動(dòng)數(shù)組后面的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

