日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#Windows服務(wù)程序開發(fā)實(shí)例介紹

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

創(chuàng)新互聯(lián)公司是專業(yè)的光明網(wǎng)站建設(shè)公司,光明接單;提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行光明網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

很多開機(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,包含兩列 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. 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)用程序的主入口點(diǎn)。   
  12. /// ﹤/summary﹥   
  13. static void Main()   
  14. {   
  15. ServiceBase[] ServicesToRun;   
  16.   
  17. // 同一進(jìn)程中可以運(yùn)行多個(gè)用戶服務(wù)。若要將   
  18. // 另一個(gè)服務(wù)添加到此進(jìn)程中,請更改下行以   
  19. // 創(chuàng)建另一個(gè)服務(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. namespace WindowsServices_AutoStart
  35. {
  36. static class Program
  37. {
  38. /// ﹤summary﹥
  39. /// 應(yīng)用程序的主入口點(diǎn)。
  40. /// ﹤/summary﹥
  41. static void Main()
  42. {
  43. ServiceBase[] ServicesToRun;
  44. // 同一進(jìn)程中可以運(yùn)行多個(gè)用戶服務(wù)。若要將
  45. // 另一個(gè)服務(wù)添加到此進(jìn)程中,請更改下行以
  46. // 創(chuàng)建另一個(gè)服務(wù)對象。例如,
  47. //
  48. //   ServicesToRun = new ServiceBase[] {
  49. new Service1(), new MySecondUserService()};
  50. //
  51. ServicesToRun = new ServiceBase[] {
  52.  new WindowsServices_AutoStart() };
  53. ServiceBase.Run(ServicesToRun);
  54. }
  55. }

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. 文件是否被改動(dòng)或刪除?\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. namespace WindowsServices_AutoStart
  111. {
  112. public partial class 
  113. WindowsServices_AutoStart : ServiceBase
  114. {
  115. public WindowsServices_AutoStart()
  116. {
  117. InitializeComponent();
  118. }
  119. string StartAppPath =""; 
  120. //@"F:\00.exe";
  121. string LogFilePath ="";
  122. // @"f:\WindowsService.txt";
  123. protected override void OnStart(string[] args)
  124. {
  125. string exePath = System.
  126. Threading.Thread.GetDomain().BaseDirectory;
  127. //
  128. if (!File.Exists(exePath + @"\ServiceAppPath.xml"))
  129. {
  130. dsAppPath ds = new dsAppPath();
  131. object[] obj=new object[2];
  132. obj[0]="0";
  133. obj[1]="0";
  134. ds.Tables["dtAppPath"].Rows.Add(obj);
  135. ds.Tables["dtAppPath"].WriteXml(
  136. exePath + @"\ServiceAppPath.xml");
  137. return;
  138. }
  139. try
  140. {
  141. dsAppPath ds = new dsAppPath();
  142. ds.Tables["dtAppPath"].ReadXml(
  143. exePath + @"\ServiceAppPath.xml");
  144. DataTable dt = ds.Tables["dtAppPath"];
  145. StartAppPath = dt.Rows[0]
  146. ["StartAppPath"].ToString();
  147. LogFilePath = dt.Rows[0]
  148. ["LogFilePath"].ToString();
  149. }
  150. catch { return; }
  151. if (File.Exists(StartAppPath))
  152. {
  153. try
  154. {
  155. Process proc = new Process();
  156. proc.StartInfo.FileName = StartAppPath; //注意路徑
  157. //proc.StartInfo.Arguments = "";
  158. proc.Start();
  159. }
  160. catch (System.Exception ex)
  161. {
  162. //MessageBox.Show(this, "
  163. 找不到幫助文件路徑。文件是否被改動(dòng)或刪除?\n"
  164.  + ex.Message, "提示", MessageBoxButtons.OK,
  165.  MessageBoxIcon.Information);
  166. }
  167. FileStream fs = new FileStream(LogFilePath,
  168.  FileMode.OpenOrCreate, FileAccess.Write);
  169. StreamWriter m_streamWriter = new StreamWriter(fs);
  170. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  171. m_streamWriter.WriteLine("WindowsService: 
  172. Service Started" + DateTime.Now.ToString() + "\n");
  173. m_streamWriter.Flush();
  174. m_streamWriter.Close();
  175. fs.Close();
  176. }
  177. }
  178. protected override void OnStop()
  179. {
  180. try
  181. {
  182. // TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。
  183. FileStream fs = new FileStream(LogFilePath, 
  184. FileMode.OpenOrCreate, FileAccess.Write);
  185. StreamWriter m_streamWriter = new StreamWriter(fs);
  186. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  187. m_streamWriter.WriteLine("WindowsService: 
  188. Service Stopped " + DateTime.Now.ToString() + "\n");
  189. m_streamWriter.Flush();
  190. m_streamWriter.Close();
  191. fs.Close();
  192. }
  193. catch
  194. {
  195. }
  196. }
  197. }
  198. }

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

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

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

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

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

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

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

installutil yourproject.exe

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

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

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

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

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

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

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

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

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

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

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


網(wǎng)頁題目:C#Windows服務(wù)程序開發(fā)實(shí)例介紹
標(biāo)題鏈接:http://www.dlmjj.cn/article/cdeepsj.html