程序输出结果
添加类(IDInfo.cs)–获取身份证中的各种信息
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;namespace IDInformation02
{class IDInfo{public string GetUserBirthday(string IDInfo){//读取出生日期string res = "";string IDcard1 = IDInfo;string strYearDate = IDcard1.Substring(6, 4);//截取第6-10位string strMonthDate = IDcard1.Substring(10, 2);//截取第6-10位string strDate = IDcard1.Substring(12, 2);//截取第6-10位res = strYearDate + '年' + strMonthDate + '月' + strDate + '日';return res;}public string GetUserAddress(string IDInfo){//读取所在地string res = "";string IDcard1 = IDInfo;StreamReader file = null;JObject home_location;//这个异常处理是失败的,后续还需改进try{//System.IO.StreamReader file = System.IO.File.OpenText(@"F:\SoftwareLearning\VS2017\C#Learing\chapter01\ConsoleApp0721\province01.json");file = System.IO.File.OpenText(@"F:\SoftwareLearning\VS2017\C#Learing\chapter01\ConsoleApp0721\province.json");JsonTextReader reader = new JsonTextReader(file);//进行字符串切割赋值home_location = (JObject)JToken.ReadFrom(reader);res = home_location[IDcard1.Substring(0, 6)].ToString();}catch (ExceptionName){res = "文件读取失败";throw;}return res;}public string GetUserGender(string IDInfo){//读取性别string res = "男";string IDcard1 = IDInfo;string strSex = IDcard1.Substring(16, 1);//截取倒数第二位 int numSex = int.Parse(strSex); //字符串型变为数值型if (numSex % 2 == 0){res = "女";}else{res = "男";}return res;}}
}
添加类(ExceptionHandling.cs)–判断身份证是不是18位数
using System;namespace IDInformation02
{class ExceptionHandling{//判断身份证是不是18位数public string GetUserNum(string IDInfo){string IDcard1 = IDInfo;string res;int num = 0;foreach (char c in IDcard1){if (Char.IsDigit(c)) ++num;}if (num == 18)res = "身份证号码正确";else{res = "身份证号码正确";}return res;}}
}
窗口程序(Form1.cs)
using System;
using System.Windows.Forms;namespace IDInformation02
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string IDcard1;IDcard1 = textBox1.Text;IDInfo idinfo = new IDInfo();textBox2.Text = idinfo.GetUserBirthday(IDcard1); //输出出生日期textBox3.Text = idinfo.GetUserAddress(IDcard1); //输出所在地textBox4.Text = idinfo.GetUserGender(IDcard1); //输出性别ExceptionHandling exceptionHandling = new ExceptionHandling(); //异常处理,还不完善(比如前六位地址输入是否正确)textBox5.Text = exceptionHandling.GetUserNum(IDcard1);}}
}
主程序(Program.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;namespace IDInformation02
{static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}
}