C#生日提醒小工具

一个很粗糙的版本,就当一个小例子看一下吧,

运行效果如下:

 

开发环境VS2017,用的WinForm,涉及一点xml,直接上图。

 

一、项目涉及的文件如下图:

 

二、每个文件内容:

1、MainForm  CS 后台

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static BirthDayWarn.BirthDayHelper;

namespace BirthDayWarn
{
    public partial class MainForm : Form
    {
        //生日提醒参数(即 几天内的生日需要提醒)
        int DaysT = int.Parse(ConfigurationManager.AppSettings["DaysT"]);

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //获取生日数据进行处理
            //加载xml,获取基本信息
             string filePath = AppDomain.CurrentDomain.BaseDirectory + "BirthDayInfo.xml";
            XDocument xdoc = null;
            try
            {
                xdoc = XDocument.Load(filePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return;
            }

            var configs = from a in xdoc.Descendants("Node")
                          select new BirthDayModel
                          {
                              Name = a.Attribute("Name").Value,
                              CardNo = a.Attribute("CardNo").Value
                          };

            var alertList = configs.ToList<BirthDayModel>();

            if (alertList == null || alertList.Count == 0)
            {
                this.lblMsg.Text = "请先配置BirthDayInfo.xml文件!";
            }
            else
            {
                string returnMsg = LeftDay(DaysT, alertList);
                if (!string.IsNullOrEmpty(returnMsg))
                {
                    this.lblMsg.Text = DaysT.ToString() + "天内,以下人员生日临近:\r\n" + returnMsg;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 2、BirthDayModel 实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BirthDayWarn
{
    /// <summary>
    /// 信息Model实体
    /// </summary>
    public class BirthDayModel
    {
        public string Name { get; set; }
        public string CardNo { get; set; }
    }
}

3、BirthDayHelper 帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BirthDayWarn
{
    public class BirthDayHelper
    {
        /// <summary>
        /// 根据传入的身份证 计算符合条件的生日日期
        /// </summary>
        /// <param name="daysT">提醒日期参数</param>
        /// <param name="list">xml数据</param>
        /// <returns></returns>
        public static string LeftDay(int daysT, List<BirthDayModel> list)
        {
            StringBuilder sb = new StringBuilder();
            int days = 0;

            foreach (var item in list)
            {
                if (!string.IsNullOrEmpty(item.CardNo) && item.CardNo.Length == 15 || item.CardNo.Length == 18)
                {
                    //处理18位的身份证号
                    if (item.CardNo.Length == 18)
                    {
                        //身份证中的月份与当前月作对比
                        if (item.CardNo.Substring(10, 2) == DateTime.Now.Month.ToString())
                        {
                            days = int.Parse(item.CardNo.Substring(12, 2)) - DateTime.Now.Day;
                            if (days >= 0 && days < daysT)
                            {
                                if (days == 0)
                                {
                                    sb.AppendLine(string.Format("今天是{0}的生日({1})", item.Name, DateTime.Now.ToString("yyyy-MM-dd")));
                                }
                                else
                                {
                                    sb.AppendLine(string.Format("距{0}的生日还有【{1}】天({2})", item.Name, days.ToString(), DateTime.Now.AddDays(days).ToString("yyyy-MM-dd")));
                                }
                            }
                        }
                    }
                    //处理15位的身份证号
                    if (item.CardNo.Length == 15)
                    {
                        //身份证中的月份与当前月作对比
                        if (item.CardNo.Substring(8, 2) == DateTime.Now.Month.ToString())
                        {
                            days = int.Parse(item.CardNo.Substring(10, 2)) - DateTime.Now.Day;
                            if (days >= 0 && days < daysT)
                            {
                                if (days == 0)
                                {
                                    sb.AppendLine(string.Format("今天是{0}的生日({1})", item.Name, DateTime.Now.ToString("yyyy-MM-dd")));
                                }
                                else
                                {
                                    sb.AppendLine(string.Format("距{0}的生日还有【{1}】天({2})", item.Name, days.ToString(), DateTime.Now.AddDays(days).ToString("yyyy-MM-dd")));
                                }
                            }
                        }
                    }
                }
            }
            return sb.ToString();
        }
    }
}

4、BirthDayInfo.xml  文件

<?xml version="1.0" encoding="utf-8" ?>
<BirthDayInfo>
  <Node Name="张三" CardNo="371302199011053148" />
  <Node Name="李四" CardNo="371302199011033148" />
</BirthDayInfo>

5、AppConfig 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <!--提前几天进行提醒-->
    <add key="DaysT"  value="7" />
  </appSettings>
</configuration>

 

编译生成后 Bin目录下 exe就可以正常运行使用,

三、将exe 添加到开机计划任务中。

打开控制面板,右上角 查看方式 改为【大图标】,找到【管理工具】打开,如下图:

打开任务计划程序,按指引步骤添加 任务计划,

 

 

 

 

 

 找到.exe 应用程序所在的位置即可,如下图:

以上设置完成,每次开机时,即可看到生日提醒

 

转载于:https://www.cnblogs.com/Violety/p/9897463.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值