自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#Windows服務(wù)程序開發(fā)實例介紹

開發(fā) 后端
C#Windows服務(wù)程序開發(fā)實例主要向你介紹一個用于開機(jī)啟動其他程序的Windows服務(wù),那么具體的實現(xiàn)的步驟是什么呢?一下的C#Windows服務(wù)程序開發(fā)實例就向你一一介紹。

C#Windows服務(wù)程序開發(fā)實例程序的目的和用途:

很多開機(jī)啟動程序僅僅加在啟動項里面,只有登陸后才真正啟動。windows服務(wù)在開機(jī)未進(jìn)行用戶登錄前就啟動了。正是利用這一點,解決一些服務(wù)器自動重啟后特定軟件也自動啟動的問題。

C#Windows服務(wù)程序開發(fā)1.

新建一個服務(wù)項目 visual C#----windows----windows服務(wù);

C#Windows服務(wù)程序開發(fā)2.

添加一個dataset(.xsd),用于存儲啟動目標(biāo)的路徑,日志路徑等。

在dataset可視化編輯中,添加一個datatable,包含兩列 StartAppPath 和 LogFilePath。分別用于存儲目標(biāo)的路徑、日志路徑。

我認(rèn)為利用dataset.xsd存儲配置參數(shù)的優(yōu)勢在于可以忽略xml解析的具體過程直接使用xml文件。

在dataset中 提供了ReadXml方法用于讀取xml文件并將其轉(zhuǎn)換成內(nèi)存中的一張datatable表,數(shù)據(jù)很容易取出來!同樣,WriteXml方法用于存儲為xml格式的文件,也僅僅需要一句話而已。

C#Windows服務(wù)程序開發(fā)3.

program.cs文件 作為程序入口,代碼如下:

  1. view plaincopy to clipboardprint?  
  2. using System.Collections.Generic;     
  3. using System.ServiceProcess;     
  4. using System.Text;     
  5.     
  6. namespace WindowsServices_AutoStart     
  7. {     
  8. static class Program     
  9. {     
  10. /// ﹤summary﹥     
  11. /// 應(yīng)用程序的主入口點。     
  12. /// ﹤/summary﹥     
  13. static void Main()     
  14. {     
  15. ServiceBase[] ServicesToRun;     
  16.     
  17. // 同一進(jìn)程中可以運(yùn)行多個用戶服務(wù)。若要將     
  18. // 另一個服務(wù)添加到此進(jìn)程中,請更改下行以     
  19. // 創(chuàng)建另一個服務(wù)對象。例如,     
  20. //     
  21. //   ServicesToRun = new ServiceBase[] {  
  22. new Service1(), new MySecondUserService()};     
  23. //     
  24. ServicesToRun = new ServiceBase[] {   
  25. new WindowsServices_AutoStart() };     
  26.     
  27. ServiceBase.Run(ServicesToRun);     
  28. }     
  29. }     
  30. }    
  31. using System.Collections.Generic;  
  32. using System.ServiceProcess;  
  33. using System.Text;  
  34.  
  35. namespace WindowsServices_AutoStart  
  36. {  
  37. static class Program  
  38. {  
  39. /// ﹤summary﹥  
  40. /// 應(yīng)用程序的主入口點。  
  41. /// ﹤/summary﹥  
  42. static void Main()  
  43. {  
  44. ServiceBase[] ServicesToRun;  
  45.  
  46. // 同一進(jìn)程中可以運(yùn)行多個用戶服務(wù)。若要將  
  47. // 另一個服務(wù)添加到此進(jìn)程中,請更改下行以  
  48. // 創(chuàng)建另一個服務(wù)對象。例如,  
  49. //  
  50. //   ServicesToRun = new ServiceBase[] {  
  51. new Service1(), new MySecondUserService()};  
  52. //  
  53. ServicesToRun = new ServiceBase[] {  
  54.  new WindowsServices_AutoStart() };  
  55.  
  56. ServiceBase.Run(ServicesToRun);  
  57. }  
  58. }  
  59. }  

C#Windows服務(wù)程序開發(fā)4.

service.cs主文件,代碼如下:

  1. view plaincopy to clipboardprint?  
  2. using System;     
  3. using System.Collections.Generic;     
  4. using System.ComponentModel;     
  5. using System.Data;     
  6. using System.IO;     
  7. using System.Diagnostics;     
  8. using System.ServiceProcess;     
  9. using System.Text;     
  10.     
  11. namespace WindowsServices_AutoStart     
  12. {     
  13. public partial class   
  14. WindowsServices_AutoStart : ServiceBase     
  15. {     
  16. public WindowsServices_AutoStart()     
  17. {     
  18. InitializeComponent();     
  19. }     
  20. string StartAppPath ="";  
  21.  //@"F:\00.exe";     
  22. string LogFilePath ="";  
  23. // @"f:\WindowsService.txt";     
  24. protected override void OnStart(string[] args)     
  25. {     
  26. string exePath = System.Threading.  
  27. Thread.GetDomain().BaseDirectory;     
  28. //     
  29. if (!File.Exists(exePath + @"\ServiceAppPath.xml"))     
  30. {     
  31. dsAppPath ds = new dsAppPath();     
  32. object[] obj=new object[2];     
  33. obj[0]="0";     
  34. obj[1]="0";     
  35. ds.Tables["dtAppPath"].Rows.Add(obj);     
  36. ds.Tables["dtAppPath"].WriteXml(  
  37. exePath + @"\ServiceAppPath.xml");     
  38. return;     
  39. }     
  40. try    
  41. {     
  42. dsAppPath ds = new dsAppPath();     
  43. ds.Tables["dtAppPath"].ReadXml(  
  44. exePath + @"\ServiceAppPath.xml");     
  45. DataTable dt = ds.Tables["dtAppPath"];     
  46. StartAppPath = dt.Rows[0]  
  47. ["StartAppPath"].ToString();     
  48. LogFilePath = dt.Rows[0]  
  49. ["LogFilePath"].ToString();     
  50. }     
  51. catch { return; }     
  52.      
  53. if (File.Exists(StartAppPath))     
  54. {     
  55. try    
  56. {     
  57. Process proc = new Process();     
  58. proc.StartInfo.FileName = StartAppPath; //注意路徑     
  59. //proc.StartInfo.Arguments = "";     
  60. proc.Start();     
  61. }     
  62. catch (System.Exception ex)     
  63. {     
  64. //MessageBox.Show(this, "找不到幫助文件路徑。  
  65. 文件是否被改動或刪除?\n" + ex.Message, "提示",  
  66.  MessageBoxButtons.OK, MessageBoxIcon.Information);     
  67. }     
  68. FileStream fs = new FileStream(LogFilePath,   
  69. FileMode.OpenOrCreate, FileAccess.Write);     
  70. StreamWriter m_streamWriter = new StreamWriter(fs);     
  71. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);     
  72. m_streamWriter.WriteLine("WindowsService:   
  73. Service Started" + DateTime.Now.ToString() + "\n");     
  74. m_streamWriter.Flush();     
  75. m_streamWriter.Close();     
  76. fs.Close();     
  77. }     
  78. }     
  79.     
  80. protected override void OnStop()     
  81. {     
  82. try    
  83. {     
  84. // TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。     
  85. FileStream fs = new FileStream(LogFilePath,  
  86.  FileMode.OpenOrCreate, FileAccess.Write);     
  87. StreamWriter m_streamWriter = new StreamWriter(fs);     
  88. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);     
  89. m_streamWriter.WriteLine("WindowsService:  
  90.  Service Stopped " + DateTime.Now.ToString() + "\n");     
  91. m_streamWriter.Flush();     
  92. m_streamWriter.Close();     
  93. fs.Close();     
  94. }     
  95. catch    
  96. {     
  97.     
  98. }     
  99. }     
  100. }     
  101. }    
  102. using System;  
  103. using System.Collections.Generic;  
  104. using System.ComponentModel;  
  105. using System.Data;  
  106. using System.IO;  
  107. using System.Diagnostics;  
  108. using System.ServiceProcess;  
  109. using System.Text;  
  110.  
  111. namespace WindowsServices_AutoStart  
  112. {  
  113. public partial class   
  114. WindowsServices_AutoStart : ServiceBase  
  115. {  
  116. public WindowsServices_AutoStart()  
  117. {  
  118. InitializeComponent();  
  119. }  
  120. string StartAppPath ="";   
  121. //@"F:\00.exe";  
  122. string LogFilePath ="";  
  123. // @"f:\WindowsService.txt";  
  124. protected override void OnStart(string[] args)  
  125. {  
  126. string exePath = System.  
  127. Threading.Thread.GetDomain().BaseDirectory;  
  128. //  
  129. if (!File.Exists(exePath + @"\ServiceAppPath.xml"))  
  130. {  
  131. dsAppPath ds = new dsAppPath();  
  132. object[] obj=new object[2];  
  133. obj[0]="0";  
  134. obj[1]="0";  
  135. ds.Tables["dtAppPath"].Rows.Add(obj);  
  136. ds.Tables["dtAppPath"].WriteXml(  
  137. exePath + @"\ServiceAppPath.xml");  
  138. return;  
  139. }  
  140. try 
  141. {  
  142. dsAppPath ds = new dsAppPath();  
  143. ds.Tables["dtAppPath"].ReadXml(  
  144. exePath + @"\ServiceAppPath.xml");  
  145. DataTable dt = ds.Tables["dtAppPath"];  
  146. StartAppPath = dt.Rows[0]  
  147. ["StartAppPath"].ToString();  
  148. LogFilePath = dt.Rows[0]  
  149. ["LogFilePath"].ToString();  
  150. }  
  151. catch { return; }  
  152.  
  153. if (File.Exists(StartAppPath))  
  154. {  
  155. try 
  156. {  
  157. Process proc = new Process();  
  158. proc.StartInfo.FileName = StartAppPath; //注意路徑  
  159. //proc.StartInfo.Arguments = "";  
  160. proc.Start();  
  161. }  
  162. catch (System.Exception ex)  
  163. {  
  164. //MessageBox.Show(this, "  
  165. 找不到幫助文件路徑。文件是否被改動或刪除?\n"  
  166.  + ex.Message, "提示", MessageBoxButtons.OK,  
  167.  MessageBoxIcon.Information);  
  168. }  
  169. FileStream fs = new FileStream(LogFilePath,  
  170.  FileMode.OpenOrCreate, FileAccess.Write);  
  171. StreamWriter m_streamWriter = new StreamWriter(fs);  
  172. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);  
  173. m_streamWriter.WriteLine("WindowsService:   
  174. Service Started" + DateTime.Now.ToString() + "\n");  
  175. m_streamWriter.Flush();  
  176. m_streamWriter.Close();  
  177. fs.Close();  
  178. }  
  179. }  
  180.  
  181. protected override void OnStop()  
  182. {  
  183. try 
  184. {  
  185. // TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。  
  186. FileStream fs = new FileStream(LogFilePath,   
  187. FileMode.OpenOrCreate, FileAccess.Write);  
  188. StreamWriter m_streamWriter = new StreamWriter(fs);  
  189. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);  
  190. m_streamWriter.WriteLine("WindowsService:   
  191. Service Stopped " + DateTime.Now.ToString() + "\n");  
  192. m_streamWriter.Flush();  
  193. m_streamWriter.Close();  
  194. fs.Close();  
  195. }  
  196. catch 
  197. {  
  198.  
  199. }  
  200. }  
  201. }  

C#Windows服務(wù)程序開發(fā)5.

啟動調(diào)試,成功時也會彈出一個對話框大致意思是提示服務(wù)需要安裝。

C#Windows服務(wù)程序開發(fā)6.

把Debug文件夾下面的.exe執(zhí)行程序,安裝為windows系統(tǒng)服務(wù),安裝方法網(wǎng)上很多介紹。我說一種常用的:

C#Windows服務(wù)程序開發(fā)之安裝服務(wù)

訪問項目中的已編譯可執(zhí)行文件所在的目錄。

用項目的輸出作為參數(shù),從命令行運(yùn)行 InstallUtil.exe。在命令行中輸入下列代碼: 

installutil yourproject.exe

C#Windows服務(wù)程序開發(fā)之卸載服務(wù) 

用項目的輸出作為參數(shù),從命令行運(yùn)行 InstallUtil.exe。 
installutil /u yourproject.exe

至此,整個服務(wù)已經(jīng)編寫,編譯,安裝完成,你可以在控制面板的管理工具的服務(wù)中,看到你編寫的服務(wù)。

C#Windows服務(wù)程序開發(fā)7.

安裝好了之后在系統(tǒng)服務(wù)列表中可以管理服務(wù),這時要注意將服務(wù)的屬性窗口----登陸----“允許于桌面交互”勾選!這樣才能在啟動了你要的目標(biāo)程序后不單單存留于進(jìn)程。在桌面上也看得到。

C#Windows服務(wù)程序開發(fā)8.

關(guān)于卸載服務(wù),目前有兩個概念:一是禁用而已;一是完全刪除服務(wù)。 前者可以通過服務(wù)管理窗口直接完成。后者則需要進(jìn)入注冊表

“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services”找到服務(wù)名稱的文件夾,整個刪掉,重新啟動電腦后,服務(wù)消失。

C#Windows服務(wù)程序開發(fā)9.

擴(kuò)展思考:經(jīng)過修改代碼,還可以實現(xiàn):啟動目標(biāo)程序之前,檢測進(jìn)程中是否存在目標(biāo)程序,存在則不再次啟動。

C#Windows服務(wù)程序開發(fā)的實例的基本內(nèi)容就向你,希望對你學(xué)習(xí)和理解C#Windows服務(wù)程序開發(fā)有所幫助。

【編輯推薦】

  1. C#Windows服務(wù)程序開發(fā)之Windows服務(wù)淺析
  2. C#Windows服務(wù)程序安裝淺析
  3. C#Windows服務(wù)程序開發(fā)的體會
  4. C#啟動windows服務(wù)的方法淺析
  5. C#windows服務(wù)狀態(tài)改變操作淺析
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-08-14 14:17:16

C#Windows服務(wù)

2009-08-14 10:50:09

Windows服務(wù)介紹

2009-08-14 14:25:09

Windows服務(wù)程序

2009-08-14 15:19:38

Windows服務(wù)程序Windows服務(wù)

2009-08-14 15:54:50

Windows服務(wù)程序C#Windows服務(wù)

2009-08-14 14:45:03

C#Windows服務(wù)

2009-08-14 15:06:08

Windows服務(wù)程序

2009-08-14 15:47:18

C#Windows服務(wù)

2009-08-14 16:48:39

C#Windows服務(wù)

2009-08-14 13:41:13

C#Windows服務(wù)

2009-08-14 17:27:30

C#Windows應(yīng)用

2009-08-14 17:36:20

C#Windows應(yīng)用

2009-08-14 14:53:55

WINDOWS服務(wù)交互

2009-08-14 17:55:52

C#Windows應(yīng)用

2009-08-14 17:43:20

C#Windows應(yīng)用

2009-08-14 11:15:19

文件監(jiān)視C#Windows服務(wù)

2009-08-14 16:13:25

C#windows服務(wù)

2009-08-14 18:00:22

C#Windows應(yīng)用

2009-08-14 18:04:59

C#Windows應(yīng)用

2009-08-14 17:51:32

C#Windows應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號