前兩天公司要做一個windows服務,要我提供下。順手就做了一個殼 現在跟大家分享下:
1.打開VS(我的是2010),新建一個項目選擇“Window 服務” :
2. 右擊 “Service1.cs 設計” 選擇 "添加安裝程序" :
3. 在 “Service1.cs 設計” 中 把 ServiceName 設置為 "ServiceTest"(用戶可以自己定義),然后 選中serviceInstaller1 把 ServiceName 設置跟之前"Service1.cs 設計"中的一樣,還有幾個屬性設置:
這些屬性的作用就不敘述了,自己去了解。
接下來 選中 serviceProcessInstaller1 設置屬性:
Ok,現在是寫服務的工作了
右擊 在 “Service1.cs 設計” 查看代碼,打開如圖:
這個是用戶要寫自己的邏輯,我這就不敘述了。
好了,現在服務是寫了 該安裝了。
新建一個window Form 程序,我就不貼新建圖了 直接看最后的圖:
現在是貼代碼了:
private void btnInstall_Click(object sender, EventArgs e) { if (!ServiceIsExisted("ServiceTest")) { try { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; ManagedInstallerClass.InstallHelper(new string[] { "WindowsServiceTest.exe" }); System.Environment.CurrentDirectory = CurrentDirectory; LabelTooptip.Text = "安裝成功!"; } catch (Exception ex) { LabelTooptip.Text = "安裝出錯:" + ex.Message; } } else { LabelTooptip.Text = "該服務已經安裝,如需重裝請先卸載!"; } } private void btnUnInstall_Click(object sender, EventArgs e) { if (ServiceIsExisted("ServiceTest")) { try { string CurrentDirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = CurrentDirectory + "\\Service"; ManagedInstallerClass.InstallHelper(new string[] {"/u" ,"WindowsServiceTest.exe" }); System.Environment.CurrentDirectory = CurrentDirectory; LabelTooptip.Text = "卸載成功!"; } catch (Exception ex) { LabelTooptip.Text = "卸載出錯:" + ex.Message; } } else { LabelTooptip.Text = "您要卸載的服務不存在!"; } } private void btnStart_Click(object sender, EventArgs e) { try { ServiceController serviceController = new ServiceController("ServiceTest"); serviceController.Start(); LabelTooptip.Text = "服務啟動成功!"; } catch (Exception ex) { LabelTooptip.Text = "服務啟動出錯:" + ex.Message; } } private void btnStop_Click(object sender, EventArgs e) { try { ServiceController serviceController = new ServiceController("ServiceTest"); if (serviceController.CanStop) serviceController.Stop(); LabelTooptip.Text = "服務停止成功!"; } catch (Exception ex) { LabelTooptip.Text = "服務停止出錯:" + ex.Message; } } private bool ServiceIsExisted(string svcName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == svcName) { return true; } } return false; }
到這里基本就OK ,但是還有一個問題就是這個程序必須已管理員運行 所以在 Form 程序的 Program.cs 里加上一段代碼, 如下代碼:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); /** * 當前用戶是管理員的時候,直接啟動應用程序 * 如果不是管理員,則使用啟動對象啟動程序,以確保使用管理員身份運行 */ //獲得當前登錄的Windows用戶標示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判斷當前登錄用戶是否為管理員 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { //如果是管理員,則直接運行 Application.Run(new Form1()); } else { //創建啟動對象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.UseShellExecute = true; startInfo.WorkingDirectory = Environment.CurrentDirectory; startInfo.FileName = Application.ExecutablePath; //設置啟動動作,確保以管理員身份運行 startInfo.Verb = "runas"; try { System.Diagnostics.Process.Start(startInfo); } catch { return; } } }
現在是大功告成了。測試如下:
在這里我聲明下,我也是看了網上的一些人的資料才完成的,當時沒有記錄人家的網址,在這里先謝謝人家提供的資料。
如果有問題或者有更好的處理歡迎指點!
附 源代碼:
http://files.cnblogs.com/files/startlearn/WindowsServiceTest.zip