• 推荐
  • 评论
  • 收藏

c#计划任务

2022-12-29    108次浏览

做一个类似计划任务的模块,在配置文件里写要执行的时间,让程序定时执行。

1.建立配置文件App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--设定每月执行计划任务的日期,先设定每月的16号,17号,25号执行-->
    <add key ="DateNum" value ="16,17,25"/>
  </appSettings>
</configuration>

2. 建立PlanWork.cs文件

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Timers;

namespace PlanWork
{
    public class PlanWork
    {
        static void Main(string[] args)
        {
            Myplan mp = new Myplan();


            //*************************************************
            //设定间隔时间是15天,测试的时候设定时间为1000纳秒
            //Timer t = new Timer(15 * 24 * 60 * 60000);
            Timer t = new Timer(1000);
            //*************************************************


            //绑定定时触发的函数
            t.Elapsed += new ElapsedEventHandler(mp.RunMyplan);
            t.Start();
            Console.ReadLine();
        }

    }
    public class Myplan
    {
        public void RunMyplan(Object source, ElapsedEventArgs e)
        {
            //读取配置文件设定的日期时间
            string SetDate = ConfigurationManager.AppSettings["DateNum"].ToString();

            //获取现在的系统时间
            DateTime nowTime = System.DateTime.Now;
            string d = nowTime.Day.ToString(); //取日期

            //比较是否符合设定的时间,SetDate中是否有d的存在 
            int i = SetDate.IndexOf(d);
            if (i >= 0)
            {
                //计划任务要执行程序
                Console.Write("\nToday is " + d + " day!");
            }

        }

    }
}

这样一个计划任务的小程序就ok了。

Asp.net:

http://book.chinaz.com/others/web/web/aspnet/index.htm

Data Access Application Block for .NET

http://www.microsoft.com/china/MSDN/library/EnterpriseDevelopment/BuildDistApp/Vsdnbdadaab_rm.mspx?mfr=true

 开源资料大全

Programming C#中文版:第4版

ASP.NET 2.0揭秘(卷2):http://book.csdn.net/bookfiles/488/

最优化ASP.NET (面向对象):http://book.csdn.net/bookfiles/90/index.html

精通基于ASP.NET 2.0的Web 2.0应用(如RSS、Blog、Tags、Web service、BBS、XML、AJAX、WIKI) :

                                             http://book.csdn.net/bookfiles/425/index.html

Expert C# 2005 Business Objects中文版 :http://book.csdn.net/bookfiles/397/

Beginning C# Objects从概念到代码 :http://book.csdn.net/bookfiles/26/index.html

C#和.NET实战:平台、语言与框架 :http://book.csdn.net/bookfiles/588/index.html

框架设计(第2版):CLR Via C# :http://book.csdn.net/bookfiles/154/index.html

C#高级编程(第4版) :http://book.csdn.net/bookfiles/140/

Effective C#中文版:改善C#程序的50种方法 :http://book.csdn.net/bookfiles/295/

ASP.NET 2.0服务器控件与组件开发高级编程 :http://book.csdn.net/bookfiles/337/index.html

 道不远人:深入解析ASP.NET 2.0控件开发 :http://book.csdn.net/bookfiles/533/


原文地址:https://www.cnblogs.com/Leo_wl/p/2224735.html