這篇文章主要介紹“C# Windows服務程序開發(fā)實例分析”,在日常操作中,相信很多人在C# Windows服務程序開發(fā)實例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C# Windows服務程序開發(fā)實例分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
五峰ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
C#Windows服務程序開發(fā)實例程序的目的和用途:
很多開機啟動程序僅僅加在啟動項里面,只有登陸后才真正啟動。windows服務在開機未進行用戶登錄前就啟動了。正是利用這一點,解決一些服務器自動重啟后特定軟件也自動啟動的問題。
C#Windows服務程序開發(fā)1.
新建一個服務項目 visual C#----windows----windows服務;
C#Windows服務程序開發(fā)2.
添加一個dataset(.xsd),用于存儲啟動目標的路徑,日志路徑等。
在dataset可視化編輯中,添加一個datatable,包含兩列 StartAppPath 和 LogFilePath。分別用于存儲目標的路徑、日志路徑。
我認為利用dataset.xsd存儲配置參數(shù)的優(yōu)勢在于可以忽略xml解析的具體過程直接使用xml文件。
在dataset中 提供了ReadXml方法用于讀取xml文件并將其轉(zhuǎn)換成內(nèi)存中的一張datatable表,數(shù)據(jù)很容易取出來!同樣,WriteXml方法用于存儲為xml格式的文件,也僅僅需要一句話而已。
C#Windows服務程序開發(fā)3.
program.cs文件 作為程序入口,代碼如下:
view plaincopy to clipboardprint? using System.Collections.Generic; using System.ServiceProcess; using System.Text; namespace WindowsServices_AutoStart { static class Program { /// ﹤summary﹥ /// 應用程序的主入口點。 /// ﹤/summary﹥ static void Main() { ServiceBase[] ServicesToRun; // 同一進程中可以運行多個用戶服務。若要將 // 另一個服務添加到此進程中,請更改下行以 // 創(chuàng)建另一個服務對象。例如, // // ServicesToRun = new ServiceBase[] { new Service1(), new MySecondUserService()}; // ServicesToRun = new ServiceBase[] { new WindowsServices_AutoStart() }; ServiceBase.Run(ServicesToRun); } } } using System.Collections.Generic; using System.ServiceProcess; using System.Text; namespace WindowsServices_AutoStart { static class Program { /// ﹤summary﹥ /// 應用程序的主入口點。 /// ﹤/summary﹥ static void Main() { ServiceBase[] ServicesToRun; // 同一進程中可以運行多個用戶服務。若要將 // 另一個服務添加到此進程中,請更改下行以 // 創(chuàng)建另一個服務對象。例如, // // ServicesToRun = new ServiceBase[] { new Service1(), new MySecondUserService()}; // ServicesToRun = new ServiceBase[] { new WindowsServices_AutoStart() }; ServiceBase.Run(ServicesToRun); } } }
C#Windows服務程序開發(fā)4.
service.cs主文件,代碼如下:
view plaincopy to clipboardprint? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Diagnostics; using System.ServiceProcess; using System.Text; namespace WindowsServices_AutoStart { public partial class WindowsServices_AutoStart : ServiceBase { public WindowsServices_AutoStart() { InitializeComponent(); } string StartAppPath =""; //@"F:\00.exe"; string LogFilePath =""; // @"f:\WindowsService.txt"; protected override void OnStart(string[] args) { string exePath = System.Threading. Thread.GetDomain().BaseDirectory; // if (!File.Exists(exePath + @"\ServiceAppPath.xml")) { dsAppPath ds = new dsAppPath(); object[] obj=new object[2]; obj[0]="0"; obj[1]="0"; ds.Tables["dtAppPath"].Rows.Add(obj); ds.Tables["dtAppPath"].WriteXml( exePath + @"\ServiceAppPath.xml"); return; } try { dsAppPath ds = new dsAppPath(); ds.Tables["dtAppPath"].ReadXml( exePath + @"\ServiceAppPath.xml"); DataTable dt = ds.Tables["dtAppPath"]; StartAppPath = dt.Rows[0] ["StartAppPath"].ToString(); LogFilePath = dt.Rows[0] ["LogFilePath"].ToString(); } catch { return; } if (File.Exists(StartAppPath)) { try { Process proc = new Process(); proc.StartInfo.FileName = StartAppPath; //注意路徑 //proc.StartInfo.Arguments = ""; proc.Start(); } catch (System.Exception ex) { //MessageBox.Show(this, "找不到幫助文件路徑。 文件是否被改動或刪除?\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } } protected override void OnStop() { try { // TODO: 在此處添加代碼以執(zhí)行停止服務所需的關閉操作。 FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("WindowsService: Service Stopped " + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } catch { } } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Diagnostics; using System.ServiceProcess; using System.Text; namespace WindowsServices_AutoStart { public partial class WindowsServices_AutoStart : ServiceBase { public WindowsServices_AutoStart() { InitializeComponent(); } string StartAppPath =""; //@"F:\00.exe"; string LogFilePath =""; // @"f:\WindowsService.txt"; protected override void OnStart(string[] args) { string exePath = System. Threading.Thread.GetDomain().BaseDirectory; // if (!File.Exists(exePath + @"\ServiceAppPath.xml")) { dsAppPath ds = new dsAppPath(); object[] obj=new object[2]; obj[0]="0"; obj[1]="0"; ds.Tables["dtAppPath"].Rows.Add(obj); ds.Tables["dtAppPath"].WriteXml( exePath + @"\ServiceAppPath.xml"); return; } try { dsAppPath ds = new dsAppPath(); ds.Tables["dtAppPath"].ReadXml( exePath + @"\ServiceAppPath.xml"); DataTable dt = ds.Tables["dtAppPath"]; StartAppPath = dt.Rows[0] ["StartAppPath"].ToString(); LogFilePath = dt.Rows[0] ["LogFilePath"].ToString(); } catch { return; } if (File.Exists(StartAppPath)) { try { Process proc = new Process(); proc.StartInfo.FileName = StartAppPath; //注意路徑 //proc.StartInfo.Arguments = ""; proc.Start(); } catch (System.Exception ex) { //MessageBox.Show(this, " 找不到幫助文件路徑。文件是否被改動或刪除?\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } } protected override void OnStop() { try { // TODO: 在此處添加代碼以執(zhí)行停止服務所需的關閉操作。 FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("WindowsService: Service Stopped " + DateTime.Now.ToString() + "\n"); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } catch { } } } }
C#Windows服務程序開發(fā)5.
啟動調(diào)試,成功時也會彈出一個對話框大致意思是提示服務需要安裝。
C#Windows服務程序開發(fā)6.
把Debug文件夾下面的.exe執(zhí)行程序,安裝為windows系統(tǒng)服務,安裝方法網(wǎng)上很多介紹。我說一種常用的:
C#Windows服務程序開發(fā)之安裝服務
訪問項目中的已編譯可執(zhí)行文件所在的目錄。
用項目的輸出作為參數(shù),從命令行運行 InstallUtil.exe。在命令行中輸入下列代碼:
installutil yourproject.exe
C#Windows服務程序開發(fā)之卸載服務
用項目的輸出作為參數(shù),從命令行運行 InstallUtil.exe。
installutil /u yourproject.exe
至此,整個服務已經(jīng)編寫,編譯,安裝完成,你可以在控制面板的管理工具的服務中,看到你編寫的服務。
C#Windows服務程序開發(fā)7.
安裝好了之后在系統(tǒng)服務列表中可以管理服務,這時要注意將服務的屬性窗口----登陸----“允許于桌面交互”勾選!這樣才能在啟動了你要的目標程序后不單單存留于進程。在桌面上也看得到。
C#Windows服務程序開發(fā)8.
關于卸載服務,目前有兩個概念:一是禁用而已;一是完全刪除服務。 前者可以通過服務管理窗口直接完成。后者則需要進入注冊表
“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services”找到服務名稱的文件夾,整個刪掉,重新啟動電腦后,服務消失。
C#Windows服務程序開發(fā)9.
擴展思考:經(jīng)過修改代碼,還可以實現(xiàn):啟動目標程序之前,檢測進程中是否存在目標程序,存在則不再次啟動。
到此,關于“C# Windows服務程序開發(fā)實例分析”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文題目:C#Windows服務程序開發(fā)實例分析
當前地址:http://aaarwkj.com/article6/ihpgig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、服務器托管、網(wǎng)站導航、關鍵詞優(yōu)化、自適應網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)