Windows服務(wù)開發(fā)實(shí)例_第1頁
Windows服務(wù)開發(fā)實(shí)例_第2頁
Windows服務(wù)開發(fā)實(shí)例_第3頁
Windows服務(wù)開發(fā)實(shí)例_第4頁
Windows服務(wù)開發(fā)實(shí)例_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、文檔來源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持C#Windows服務(wù)程序開發(fā)實(shí)例介紹C#Windows服務(wù)程序開發(fā)實(shí)例程序的目的和用途:很多開機(jī)啟動(dòng)程序僅僅加在啟動(dòng)項(xiàng)里面,只有登陸后才真正啟動(dòng)。windows服務(wù)在開機(jī)未進(jìn)行用戶登錄前就啟動(dòng)了。正是利用這一點(diǎn),解決一些服務(wù)器自動(dòng)重啟后特定軟件也自動(dòng)啟動(dòng)的問題。C#Windows服務(wù)程序開發(fā)1.新建一個(gè)服務(wù)項(xiàng)目visual C#-windows-windows服務(wù);C#Windows服務(wù)程序開發(fā)2.添加一個(gè)dataset (.xsd ),用于存儲(chǔ)啟動(dòng)目標(biāo)的路徑,日志路徑等。在dataset可視化編輯中,添加一個(gè) datatable

2、,包含兩列 StartAppPath 和 LogFilePath。分別用于存儲(chǔ)目標(biāo)的路徑、日志路徑。我認(rèn)為利用dataset.xsd 存儲(chǔ)配置參數(shù)的優(yōu)勢在于可以忽略xml解析的具體過程直接使用xml文件。在dataset中提供了 ReadXml方法用于讀取xml文件并將其轉(zhuǎn)換成內(nèi)存中的一張 datatable 表,數(shù)據(jù)很容易取出來!同樣, WriteXml方法用于存儲(chǔ)為xml格式的文件,也僅 僅需要一句話而已。C#Windows服務(wù)程序開發(fā)3.program.cs文件 作為程序入口,代碼如下:1. using;2. usingSystem.ServiceProcess;3. usingSyst

3、em.Text;4. namespace WindowsServices_AutoStart5. 6. staticclass Program7. 8. /< summary>9. /應(yīng)用程序的主入口點(diǎn)。10. /< /summary>11. staticvoid Main()12. 13. ServiceBase口 ServicesToRun;14. /同一進(jìn)程中可以運(yùn)行多個(gè)用戶服務(wù)。若要將15. / 另一個(gè)服務(wù)添加到此進(jìn)程中,請更改下行以16. /創(chuàng)建另一個(gè)服務(wù)對象。例如,17. /18. / ServicesToRun = new ServiceBase 19.

4、new Service1(), new MySecondUserService();20. /21. ServicesToRun = new ServiceBase22. new WindowsServices_AutoStart();23. ServiceBase.Run(ServicesToRun);24. 25. 26. 27. using;28. usingSystem.ServiceProcess;29. usingSystem.Text;30. namespace WindowsServices_AutoStart31. 32. staticclass Program33. 34.

5、 /< summary>35. /應(yīng)用程序的主入口點(diǎn)。36. /< /summary>37. staticvoid Main()38. 39. ServiceBase口 ServicesToRun;40. /同一進(jìn)程中可以運(yùn)行多個(gè)用戶服務(wù)。若要將41. /另一個(gè)服務(wù)添加到此進(jìn)程中,請更改下行以42. /創(chuàng)建另一個(gè)服務(wù)對象。例如,43. /44. / ServicesToRun = new ServiceBase 45. new Service1(),new MySecondUserService();46. /47. ServicesToRun =new Service

6、Base 48. new WindowsServices_AutoStart();49. ServiceBase.Run(ServicesToRun);50. 51. 52. C#Windows服務(wù)程序開發(fā)4.service.cs主文件,代碼如下:53. view plaincopy to clipboardprint?54. usingSystem;55. using;56. using System.ComponentModel;57. usingSystem.Data;58. usingSystem.IO;59. usingSystem.Diagnostics;60. usingSyst

7、em.ServiceProcess;61. usingSystem.Text;62. namespace WindowsServices_AutoStart63. 64. public partial class65. WindowsServices_AutoStart: ServiceBase66. 67. public WindowsServices_AutoStart()68. 69. InitializeComponent();70. 71. string StartAppPath =""72. /"F:00.exe"73. string Log

8、FilePath =""74. / "f:WindowsService.txt"75. protected override void OnStart( string args)76. 77. string exePath = System.Threading.78. Thread.GetDomain().BaseDirectory;79. /80. if (!File.Exists(exePath + "ServiceAppPath.xml")81. 82. dsAppPath ds = new dsAppPath();83. ob

9、ject obj=new object 2;84. obj0="0"85. obj1="0"86. ds.Tables "dtAppPath" .Rows.Add(obj);87. ds.Tables"dtAppPath".WriteXml(88. exePath + "ServiceAppPath.xml" );89. return ;90. 91. try92. 93. dsAppPathds =newdsAppPath();94. ds.Tables"dtAppPath"

10、;.ReadXml(95. exePath + "ServiceAppPath.xml");96. DataTable dt = ds.Tables"dtAppPath"97. StartAppPath = dt.Rows098. "StartAppPath".ToString();99. LogFilePath = dt.Rows0100. "LogFilePath".ToString();101. 102. catch return; 103. if (File.Exists(StartAppPath)104.

11、 105. try106. 107. Process proc = new Process();108. = StartAppPath; / 注意路徑109. / =""110. proc.Start();111. 112. catch (System.Exception ex)113. 114. /MessageBox.Show(this,"找不到幫助文件路徑。115. 文件是否被改動(dòng)或刪除?n " + ex.Message,"提示",116. MessageBoxButtons.OK, MessageBoxIcon.Informa

12、tion);117. 118. FileStream fs = new FileStream(LogFilePath,119. FileMode.OpenOrCreate, FileAccess.Write);120. StreamWriter m_streamWriter =new StreamWriter(fs);121. m_, SeekOrigin.End);122. m_streamWriter.WriteLine("WindowsService:123. Service Started " + + "n");124. m_streamWrit

13、er.Flush();125. m_streamWriter.Close();126. fs.Close();127. 128. 129. protected override void OnStop()130. 131. try132. 133. / TODO:在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。134. FileStream fs = new FileStream(LogFilePath,135. FileMode.OpenOrCreate, FileAccess.Write);136. StreamWriter m_streamWriter =new StreamWriter(f

14、s);137. m_, SeekOrigin.End);138. m_streamWriter.WriteLine("WindowsService:139. Service Stopped " + + "n");140. m_streamWriter.Flush();141. m_streamWriter.Close();142. fs.Close();143. 144. catch145. 146. 147. 148. 149. 150. usingSystem;151. using;152. using System.ComponentModel;1

15、53. usingSystem.Data;154. usingSystem.IO;155. usingSystem.Diagnostics;156. usingSystem.ServiceProcess;157. usingSystem.Text;158. namespace WindowsServices_AutoStart159. 160. public partial class161. WindowsServices_AutoStart : ServiceBase162. 163. public WindowsServices_AutoStart()164. 165. Initiali

16、zeComponent();166. 167. string StartAppPath =""168. /"F:00.exe"169. string LogFilePath =""170. / "f:WindowsService.txt"171. protected override void OnStart( string args)172. 173. string exePath = System.174. ;175. /)176. if (!File.Exists(exePath + "Servic

17、eAppPath.xml"177. 178. dsAppPath ds = new dsAppPath();179. object obj= new object 2;180. obj0="0"181. obj1="0"182. ds.Tables"dtAppPath".Rows.Add(obj);183. ds.Tables"dtAppPath".WriteXml(184. exePath + "ServiceAppPath.xml" );185. return ;186. 187.

18、 try188. 189. dsAppPath ds = new dsAppPath();190. ds.Tables"dtAppPath".ReadXml(191. exePath + "ServiceAppPath.xml" );192. DataTable dt = ds.Tables"dtAppPath" ;193. StartAppPath = dt.Rows0194. "StartAppPath" .ToString();195. LogFilePath = dt.Rows0196. "Log

19、FilePath".ToString();197. 198. catch return; 199. if (File.Exists(StartAppPath)200. 201. try202. 203. Process proc = new Process();204. = StartAppPath; / 注意路徑205. / =""206. proc.Start();207. 208. catch (System.Exception ex)209. 210. /MessageBox.Show(this,”211. 找不到幫助文件路徑。文件是否被改動(dòng)或刪除?n&q

20、uot;212. + ex.Message, "提示",MessageBoxButtons.OK,213. MessageBoxIcon.Information);214. 215. FileStream fs = new FileStream(LogFilePath,216. FileMode.OpenOrCreate, FileAccess.Write);217. StreamWriter m_streamWriter =new StreamWriter(fs);218. m_, SeekOrigin.End);219. m_streamWriter.WriteLine

21、("WindowsService:220. Service Started " + +"n");221. m_streamWriter.Flush();222. m_streamWriter.Close();223. fs.Close();224. 225. 226. protected override void OnStop()227. 228. try229. 230. / TODO:在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。231. FileStream fs = new FileStream(LogFilePath,232. FileMode

22、.OpenOrCreate, FileAccess.Write);233. StreamWriter m_streamWriter =new StreamWriter(fs);234. m_, SeekOrigin.End);235. m_streamWriter.WriteLine("WindowsService:236. Service Stopped " + +"n");237. m_streamWriter.Flush();238. m_streamWriter.Close();239. fs.Close();240. 241. catch242. 243. 244. 245. 246. C#Windows服務(wù)程序開發(fā)5.啟動(dòng)調(diào)試,成功時(shí)也會(huì)彈

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論