成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#Windows服務程序開發實例介紹

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

C#Windows服務程序開發實例程序的目的和用途:

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

C#Windows服務程序開發1.

新建一個服務項目 visual C#----windows----windows服務;

C#Windows服務程序開發2.

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

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

我認為利用dataset.xsd存儲配置參數的優勢在于可以忽略xml解析的具體過程直接使用xml文件。

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

C#Windows服務程序開發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. /// 應用程序的主入口點。     
  12. /// ﹤/summary﹥     
  13. static void Main()     
  14. {     
  15. ServiceBase[] ServicesToRun;     
  16.     
  17. // 同一進程中可以運行多個用戶服務。若要將     
  18. // 另一個服務添加到此進程中,請更改下行以     
  19. // 創建另一個服務對象。例如,     
  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. /// 應用程序的主入口點。  
  41. /// ﹤/summary﹥  
  42. static void Main()  
  43. {  
  44. ServiceBase[] ServicesToRun;  
  45.  
  46. // 同一進程中可以運行多個用戶服務。若要將  
  47. // 另一個服務添加到此進程中,請更改下行以  
  48. // 創建另一個服務對象。例如,  
  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服務程序開發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: 在此處添加代碼以執行停止服務所需的關閉操作。     
  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: 在此處添加代碼以執行停止服務所需的關閉操作。  
  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服務程序開發5.

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

C#Windows服務程序開發6.

把Debug文件夾下面的.exe執行程序,安裝為windows系統服務,安裝方法網上很多介紹。我說一種常用的:

C#Windows服務程序開發之安裝服務

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

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

installutil yourproject.exe

C#Windows服務程序開發之卸載服務 

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

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

C#Windows服務程序開發7.

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

C#Windows服務程序開發8.

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

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

C#Windows服務程序開發9.

擴展思考:經過修改代碼,還可以實現:啟動目標程序之前,檢測進程中是否存在目標程序,存在則不再次啟動。

C#Windows服務程序開發的實例的基本內容就向你,希望對你學習和理解C#Windows服務程序開發有所幫助。

【編輯推薦】

  1. C#Windows服務程序開發之Windows服務淺析
  2. C#Windows服務程序安裝淺析
  3. C#Windows服務程序開發的體會
  4. C#啟動windows服務的方法淺析
  5. C#windows服務狀態改變操作淺析
責任編輯:仲衡 來源: 百度空間
相關推薦

2009-08-14 14:17:16

C#Windows服務

2009-08-14 10:50:09

Windows服務介紹

2009-08-14 14:25:09

Windows服務程序

2009-08-14 15:19:38

Windows服務程序Windows服務

2009-08-14 15:54:50

Windows服務程序C#Windows服務

2009-08-14 14:45:03

C#Windows服務

2009-08-14 15:47:18

C#Windows服務

2009-08-14 15:06:08

Windows服務程序

2009-08-14 16:48:39

C#Windows服務

2009-08-14 13:41:13

C#Windows服務

2009-08-14 17:27:30

C#Windows應用

2009-08-14 17:36:20

C#Windows應用

2009-08-14 14:53:55

WINDOWS服務交互

2009-08-14 17:55:52

C#Windows應用

2009-08-14 17:43:20

C#Windows應用

2009-08-14 11:15:19

文件監視C#Windows服務

2009-08-14 16:13:25

C#windows服務

2009-08-14 18:00:22

C#Windows應用

2009-08-14 18:04:59

C#Windows應用

2009-08-14 17:51:32

C#Windows應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 狠狠操狠狠操 | 91久久精品一区 | 欧美日一区二区 | 黑人巨大精品欧美一区二区免费 | 中国毛片免费 | 久久久久久艹 | 美女在线国产 | 日韩日韩日韩日韩日韩日韩日韩 | 欧美亚洲视频在线观看 | 欧美片网站免费 | 国产成人精品一区二区三区在线 | 欧美日韩综合 | 成人欧美一区二区三区在线播放 | 亚洲一区二区三区在线观看免费 | 激情av网站 | 草樱av| 日本a∨视频 | 欧美一区二区成人 | 精品国产乱码一区二区三区a | 日韩高清三区 | 天堂一区二区三区 | 欧美在线视频免费 | 自拍偷拍亚洲欧美 | 久久99精品久久久 | 三级在线免费观看 | 一区二区三区成人 | 亚洲国产18 | 国产精品久久久久久一区二区三区 | 成人午夜精品一区二区三区 | 日韩av免费在线观看 | 综合一区| 国产精品日韩一区二区 | 九色网址| 国产一区二区三区四区五区加勒比 | 日本免费一区二区三区四区 | 国产精品明星裸体写真集 | 中文字幕免费在线观看 | 国产日韩欧美电影 | 久久免费高清视频 | 国产精品久久久久婷婷二区次 |