一個很粗糙的版本,就當一個小例子看一下吧,
運行效果如下:
開發環境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 應用程序所在的位置即可,如下圖:
以上設置完成,每次開機時,即可看到生日提醒