生成DAT文件的WindowService服务

写一个生成DAT文件的WindowService服务

以下大神写的WindowService服务对于我没有写过的很有用:
1、https://www.cnblogs.com/charlie-chen2016/p/8031774.html
2、https://www.cnblogs.com/heifengwll/p/6243607.html?utm_source=itdadao&utm_medium=referral(里面有如何将服务打包的详细介绍)
3、https://www.cnblogs.com/HDK2016/p/8056977.html

注:如果生成的windows服务里用到模板文件、xml文件 需要手动将这两个文件复制到安装路径

生成DAT文件AutomaticService.cs

//构造函数
 public AutomaticService()
 {
   InitializeComponent();
   System.Timers.Timer timer = new System.Timers.Timer();
   timer.Enabled = true;
   timer.AutoReset = true;
   timer.Interval = 60000;//执行间隔时间,单位为毫秒;此时时间间隔为1分钟  
   timer.Start();
   timer.Elapsed += new ElapsedEventHandler(GetDat);  
   //System.Threading.Timer  _timer = new System.Threading.Timer(AsyncRun, null, 60000, Timeout.Infinite);   // 5分钟执行一次
 }
 protected override void OnStart(string[] args)
 {
    LogHelper.Info("服务已经启动");
 }
 protected override void OnStop() {
    LogHelper.Info("服务已经停止");
 }
 //每晚19:30运行
private void GetDat(object source, ElapsedEventArgs e
{
  //Debugger.Launch() 加入这句话可以调试服务 前提是已经安装了此服务
  if (DateTime.Now.Hour == 19 && DateTime.Now.Minute == 30)  //如果当前时间是19点30分
  {
    LogHelper.Begin();  // 计算执行时间
    List<DatInfo> datList = DapperHelper.Select<DatInfo>("select qian,fpno,xfpno from DatInfo a where a.isok =0", null);
    if (datList.Count > 0)
    {
       string strDatFileName = "HTPC" + DateTime.Now.ToString("yyMMdd") + "_" + DateTime.Now.ToString("HHmmss") + ".dat";
       string strPath = "C://******数据//*****库//装箱*******";
        if (!Directory.Exists(strPath)) //没有该路径创建
        {
           Directory.CreateDirectory(strPath);
        }
        //创建DAT文件
       FileStream fs = new FileStream(strPath + "//" + strDatFileName, FileMode.Create);
       //中文乱码预防
       StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
        try
        {
            string strDatInfo = string.Empty;
            datList.ForEach(x =>
            {
                string strContent = x.qian + "\t" + x.fpno + "\t" + x.xfpno ;
                strDatInfo = (string.IsNullOrEmpty(strDatInfo)) ? strContent : strDatInfo + "\r\n" + strContent;
           });
            sw.Write(strDatInfo);//字符串写入流
             int num = DapperHelper.Scalar<int>("update DatInfo set filename='" + strDatFileName + "', isok =1  where isok = 0 and filename ='' ", null);
             LogHelper.EndInfo("定时任务执行成功");
        }
        catch(SerializationException ex){
            LogHelper.EndError("定时任务执行失败", ex);
        }
        finally{
           sw.Close();
        }
    }
  }
}

生成的DAT文件格式
Qian1 fpno1 xfpno1
Qian2 fpno2 xfpno2

安装-Install.bat

cd /d %~dp0
SET regpath=%cd%
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe %regpath%\bin\Debug\AutomaticService.exe
Net Start AutomaticService
::echo "开机自动运行"
sc config AutomaticService start= AUTO
::echo "允许服务与桌面交互"
sc config AutomaticService type= interact type= own
pause

卸载-Uninstall.bat

cd /d %~dp0
SET regpath=%cd%
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u %regpath%\bin\Debug\AutomaticService.exe
pause

如果没有生成日志文件,请检查
1、log4net、Log4Net.Async这两个是否都引用了
2、将自己添加的log4net.config文件右击属性-复制到输出目录选为“始终复制”
3、AssemblyInfo.cs文件里添加:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = “log4net.config”, ConfigFileExtension = “config”, Watch = true)]
4、Program.cs(应用程序的主入口点)
static void Main(){log4net.Config.XmlConfigurator.Configure();}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值