建立乙個Windows Service 程式

2021-09-08 04:48:16 字數 1630 閱讀 7059

1.新建windows專案,選擇"windows服務"型別的專案。

2.在生成的service1.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.timers;

using system.io;

namespace windowsservice1

protected override void onstart(string args)

protected override void onstop()

void timer_elapsed(object sender, elapsedeventargs e)

else

sw.close();}}

}

3.在和service1.cs這個專案下在新建乙個安裝程式類installer1.cs,**如下:

using system;

using system.collections;

using system.collections.generic;

using system.componentmodel;

using system.configuration.install;

using system.linq;

namespace windowsservice1);}

}}

4.生成工程,在bin目錄下會生成exe檔案。如果直接執行exe檔案的話,是不能執行的,需要使用安裝windows服務用到乙個名為installutil.exe的命令列工具,開啟命令列工具,轉到installutil.exe的目錄下,對應的目錄為:c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe,然後執行installutil.exe+待執行的exe檔案的目錄,如:installutil.exe f:\myproject\windowsservice1\windowsservice1\bin\debug\windowsservice1.exe。執行成功後,會在windows的服務中,出現了剛剛新增的服務的名稱。

開啟windows服務列表方式:執行 --> 輸入services.msc 

在列表中查詢乙個叫mytestwindowsservice1 的服務,這個就是你建立安裝的服務

5.啟動該服務,這時開啟bin\debug資料夾,發現已經生成了乙個test.txt的檔案,裡面記錄了時間。這說明服務已經正式開始執行。

6.解除安裝服務的操作也和簡單,開啟命令列工具,轉到c:\windows\microsoft.net\framework\v4.0.30319目錄,然後執行installutil.exe -u f:\myproject\windowsservice1\windowsservice1\bin\debug\windowsservice1.exe命令就可以了。

建立乙個類

c 是一門物件導向的程式語言,而物件導向的基礎就是類 使用c 建立乙個student類 class student 學生類 輸出學生的資訊 void outputstudent void void student input char name,int age,int no void student...

建立乙個image

ceph 版本 0.94 7rbd命令操作的資源有volume 預設 snap lock三類資源 using rbd to create empty image rbd create size 1024000 test1 ceph src rbd.cc main code section 定義一些後...

建立乙個raymarching

shadertoy 檢視 先解釋一波raymarching,raymatching是一種計算機圖形渲染方式,但它的潛力仍未被完全發掘。raymatching一般用於渲染體積紋理 高度圖以及解析曲面。如今,大多數遊戲用opengl或direct3d directx 來使用顯示卡的硬體加速器繪製多邊形,...