栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

VS2013创建Windows服务与调试服务的图文方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

VS2013创建Windows服务与调试服务的图文方法

1、创建Windows服务

 

说明:

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

b)DisplayName 服务显示名称,直接显示到Windows服务列表中的名称;

c)ServiceName 服务进程名称,安装与卸载服务时的唯一标识。

单击“serviceProcessInstaller1”,在其属性窗口中设置Account帐号方式,建议为LocalService(当然也可以Account属性改为 LocalSystem,这样,不论是以哪个用户登录的系统,服务总会启动)。

编写安装和卸载脚本,并将放在bin/debug或bin/Release文件夹下。

安装脚本

%SystemRoot%Microsoft.NETframeworkv4.0.30319installutil.exe %~dp0exe程序的名称.exe
Net Start 服务名称
sc config 服务名称 start= auto
pause

这里注意,在exe程序的名称前面有 %~dp0 这是代表当前位置

服务名称 对应 上面我们创建服务时ServerName的名称

卸载脚本

%SystemRoot%Microsoft.NETframeworkv4.0.30319installutil.exe /u %~dp0exe程序的名称.exe
pause

同时还要注意一下,本人用的.NET4.0的版本,所以Microsoft.NETframeworkv4.0.30319installutil.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") + "rn");
      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") + "rn");
      base.onPause();
    }
    protected override void onContinue()
    {
#if DEBUG
      if (!Debugger.IsAttached)
 Debugger.Launch();
      Debugger.Break();
#endif
      WriteLog("服务恢复,时间:" + DateTime.Now.ToString("HH:mm:ss") + "rn");
      base.onContinue();
    }

    protected override void onShutdown()
    {
      WriteLog("计算机关闭,时间:" + DateTime.Now.ToString("HH:mm:ss") + "rn");
      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") + "rn";
      }
      catch (Exception ex)
      {
 result = "执行失败,原因:" + ex.Message + "rn";
      }
      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") + "rn");
    }
    /// 
    /// 日志记录
    /// 
    /// 
    private void WriteLog(string logInfo)
    {
      try
      {
 string logDirectory = AppDomain.CurrentDomain.baseDirectory + "\Logs";
 if (!Directory.Exists(logDirectory))
 {
   Directory.CreateDirectory(logDirectory);
 }
 string filePath = logDirectory + "\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
 File.AppendAllText(filePath, logInfo);
      }
      catch
      {

      }
    }
  }

以上就是关于VS2013创建Windows服务与调试服务的全部内容了,希望大家以后多多支持考高分网

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号