国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

VS2013創建Windows服務與調試服務的圖文方法

2020-01-24 00:45:44
字體:
來源:轉載
供稿:網友

1、創建Windows服務

 

說明:

a)Description 服務描述,直接顯示到Windows服務列表中的描述;

b)DisplayName 服務顯示名稱,直接顯示到Windows服務列表中的名稱;

c)ServiceName 服務進程名稱,安裝與卸載服務時的唯一標識。

單擊“serviceProcessInstaller1”,在其屬性窗口中設置Account帳號方式,建議為LocalService(當然也可以Account屬性改為 LocalSystem,這樣,不論是以哪個用戶登錄的系統,服務總會啟動)。

編寫安裝和卸載腳本,并將放在bin/debug或bin/Release文件夾下。

安裝腳本

%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe %~dp0exe程序的名稱.exeNet Start 服務名稱sc config 服務名稱 start= autopause

這里注意,在exe程序的名稱前面有 %~dp0 這是代表當前位置

服務名稱 對應 上面我們創建服務時ServerName的名稱

卸載腳本

%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe /u %~dp0exe程序的名稱.exepause

同時還要注意一下,本人用的.NET4.0的版本,所以/Microsoft.NET/Framework/v4.0.30319/installutil.exe 這一段要根據你機器安裝.NET的版本來定。

其實腳本主要是通過installutil.exe 來進行安裝和卸載服務的,同時此處涉及的批處理命令不多。

2、調試windows服務

在項目中不用啟動windows服務項目,而是直接附加進程來進行調試。

 

在可用進程中,查找到你剛才通過腳本安裝的服務就可以了。

再發一個寫入服務代碼的Demo

public partial class MMSServer : ServiceBase  {    private Timer time = new Timer();    public MMSServer()    {      InitializeComponent();    }    protected override void OnStart(string[] args)    {#if DEBUG      if (!Debugger.IsAttached)        Debugger.Launch();      Debugger.Break();#endif      WriteLog("服務啟動,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n");      time.Elapsed += new ElapsedEventHandler(MethodEvent);      time.Interval = 3 * 1000;      time.Start();    }    protected override void OnPause()    {#if DEBUG      if (!Debugger.IsAttached)        Debugger.Launch();      Debugger.Break();#endif      WriteLog("服務暫停,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n");      base.OnPause();    }    protected override void OnContinue()    {#if DEBUG      if (!Debugger.IsAttached)        Debugger.Launch();      Debugger.Break();#endif      WriteLog("服務恢復,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n");      base.OnContinue();    }    protected override void OnShutdown()    {      WriteLog("計算機關閉,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n");      base.OnShutdown();    }    private void MethodEvent(object source, System.Timers.ElapsedEventArgs e)    {      time.Enabled = false;      string result = string.Empty;      try      {        //.........        result = "執行成功,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n";      }      catch (Exception ex)      {        result = "執行失敗,原因:" + ex.Message + "/r/n";      }      finally      {        WriteLog(result);        time.Enabled = true;      }    }    protected override void OnStop()    {#if DEBUG      if (!Debugger.IsAttached)        Debugger.Launch();      Debugger.Break();#endif      WriteLog("服務停止,時間:" + DateTime.Now.ToString("HH:mm:ss") + "/r/n");    }    /// <summary>    /// 日志記錄    /// </summary>    /// <param name="logInfo"></param>    private void WriteLog(string logInfo)    {      try      {        string logDirectory = AppDomain.CurrentDomain.BaseDirectory + "http://Logs";        if (!Directory.Exists(logDirectory))        {          Directory.CreateDirectory(logDirectory);        }        string filePath = logDirectory + "http://" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";        File.AppendAllText(filePath, logInfo);      }      catch      {      }    }  }

以上就是關于VS2013創建Windows服務與調試服務的全部內容了,希望大家以后多多支持武林網

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涞源县| 新民市| 永春县| 洪泽县| 盈江县| 揭阳市| 南漳县| 大连市| 华坪县| 东乌| 芜湖县| 青海省| 延吉市| 太谷县| 阳江市| 延庆县| 隆昌县| 凤台县| 乐安县| 洪洞县| 桐庐县| 林甸县| 大洼县| 措美县| 涡阳县| 黔南| 海阳市| 龙陵县| 中西区| 定南县| 黔东| 加查县| 华蓥市| 上蔡县| 新安县| 揭阳市| 竹北市| 漳浦县| 灵台县| 禹城市| 环江|