基本信息
源码名称:C#身份证读取(精伦C#身份证读取实例)
源码大小:23.74M
文件格式:.rar
开发语言:C#
更新时间:2018-07-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using cn.zhm.common;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Timers.Timer t;
        
        public Form1()
        {
            InitializeComponent();
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        }
        [DllImport("Sdtapi.dll")]
        private static extern int InitComm(int iPort);
        [DllImport("Sdtapi.dll")]
        private static extern int Authenticate();
        [DllImport("Sdtapi.dll")]
        private static extern int ReadBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,
                                                    StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,
                                                        StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd);
        [DllImport("Sdtapi.dll")]
        private static extern int CloseComm();
        [DllImport("Sdtapi.dll")]
        private static extern int ReadBaseMsg(byte[] pMsg, ref int len);
        [DllImport("Sdtapi.dll")]
        private static extern int ReadBaseMsgW(byte[] pMsg, ref int len);

        [DllImport("Sdtapi.dll")]
        private static extern int ReadIINSNDN(byte[] pMsg);

        [DllImport("Sdtapi.dll")]
        private static extern int Routon_ReadIINSNDN(byte[] pMsg);

        bool flag = true;
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder Name = new StringBuilder(31);
            StringBuilder Gender = new StringBuilder(3);
            StringBuilder Folk = new StringBuilder(10);
            StringBuilder BirthDay = new StringBuilder(9);
            StringBuilder Code = new StringBuilder(19);
            StringBuilder Address = new StringBuilder(71);
            StringBuilder Agency = new StringBuilder(31);
            StringBuilder ExpireStart = new StringBuilder(9);
            StringBuilder ExpireEnd = new StringBuilder(9);
            byte[] pMsg = new byte[256];
            int len = 0;
            string[] temp;
            string[] baseinfo = new string[9];
            char[] param = { '\0' };

            //ConfigClass config = new ConfigClass();
           string[] arr= ConfigClass.Readconfig("config").Split('|');
           int COM = string.IsNullOrEmpty(arr[0]) ? 1001 : int.Parse(arr[0]);
           if (arr.Length > 1)
           {
               label1.Text = arr[1]   "欢迎您!";
           }
            //打开端口
            int intOpenRet = InitComm(COM);
            if (intOpenRet != 1)
            {
                if (flag)
                {
                    MessageBox.Show("阅读机具未连接");
                    
                }
                flag = false;
                return;
            }
            flag = true;
            ////卡认证
            int intReadRet = Authenticate();
            //if (intReadRet != 1)
            //{
            //    //MessageBox.Show("卡认证失败");
            //    CloseComm();
            //    return;
            //}
            //三种方式读取基本信息
//ReadBaseInfos(推荐使用)

            int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
            if (intReadBaseInfosRet != 1)
            {
                //MessageBox.Show("读卡失败");
                int intCloseRet = CloseComm();
                return;
            }

            label4.Text = Code.ToString();

            label6.Text = Name.ToString();

            label8.Text = Gender.ToString();

            label10.Text = Folk.ToString();

            label12.Text = BirthDay.ToString();

            label14.Text = Address.ToString();

            label16.Text = Agency.ToString();

            label18.Text = ExpireStart.ToString();

            label20.Text = ExpireEnd.ToString();

            pictureBox1.Load("photo.bmp");
            pictureBox1.Refresh();
            byte[] pMsg1 = new byte[256];

//            int aa = ReadIINSNDN(pMsg1);
//            //int aa = ReadIINSNDN(pMsg1);
            
////ReadBaseMsg(不推荐使用)

//            //intReadBaseInfosRet = ReadBaseMsg(pMsg, ref len);
//            //if (intReadBaseInfosRet != 1)
//               if (aa != 1)
//               {
//                   MessageBox.Show("读卡失败");
//                   CloseComm();
//                   return;
//               }
               //temp = System.Text.Encoding.Default.GetString(pMsg1).Split(param);
               //listBox1.Items.Add(temp);
               //listBox1.Items.Add("-------------");
               //      int i = 0;
               //      foreach (string s in temp)
               //      {
               //          if (s.Length == 0)
               //              continue;
               //          baseinfo[i  ] = s;
               //      }
               //      listBox1.Items.Add(temp.ToString());
               //      listBox1.Items.Add(baseinfo[0]);

                     /*                label4.Text = baseinfo[5];

                                    label6.Text = baseinfo[0];

                                    label8.Text = baseinfo[1];

                                    label10.Text = baseinfo[2];

                                    label12.Text = baseinfo[3];

                                    label14.Text = baseinfo[4];

                                    label16.Text = baseinfo[6];

                                    label18.Text = baseinfo[7];

                                    label20.Text = baseinfo[8];

                                    pictureBox1.Load("photo.bmp"); 
                        */
//ReadBaseMsgW
           
                        //intReadBaseInfosRet = ReadBaseMsgW(pMsg, ref len);
                        //if (intReadBaseInfosRet != 1)
                        //{
                        //    MessageBox.Show("读卡失败");
                        //    CloseComm();
                        //    return;
                        //}
                        //string temp1 = System.Text.Encoding.Unicode.GetString(pMsg);
                        //listBox1.Items.Add(temp1);
                        //listBox1.Items.Add("-------------");
                        /* label4.Text = temp1.Substring(61, 18); 

                        label6.Text = temp1.Substring(0, 15);

                        label8.Text = temp1.Substring(15, 1);

                        label10.Text = temp1.Substring(16, 2);

                        label12.Text = temp1.Substring(18, 8);

                        label14.Text = temp1.Substring(26, 35);

                        label16.Text = temp1.Substring(79, 15);

                        label18.Text = temp1.Substring(94, 8);

                        label20.Text = temp1.Substring(102, 8);

                        pictureBox1.Load("photo.bmp"); 
            */
            //关闭端口
            int intCloseRet1 = CloseComm();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form11 objfrm = new Form11();
            objfrm.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 objfrm1 = new Form3();
            objfrm1.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Visible = false;
            button2.Visible = false;
            //button3.Visible = false;
            //ConfigClass config = new ConfigClass();

            string[] arr = ConfigClass.Readconfig("config").Split('|');

            if (arr.Length > 1)
            {
                label1.Text = arr[1] "欢迎您!";
            }
            t = new System.Timers.Timer(5000);
            t.Elapsed  = new System.Timers.ElapsedEventHandler(theout1);//到达时间的时候执行事件;
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);

            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
            //如果不写下面这句会有一个异常。
            //异常:线程间操作无效: 从不是创建控件"richtextbox"的线程访问它
            //但这不是最好的方法。如果只有一个进程调用richtextbox而已。就可以用下面这句
            //如果有多个线程调用richtextbox等控件。就要用委托。见thread第二种方法
            
      


            Control.CheckForIllegalCrossThreadCalls = false;
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
            player.SoundLocation = Application.StartupPath   "//aa.wav";
            player.LoadAsync();
            player.PlaySync();
            timer1.Enabled = true;//开始滚动
        }
        private void theout1(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }
        private void theout(object sender, EventArgs e)
        {
            try
            {
                StringBuilder Name = new StringBuilder(31);
                StringBuilder Gender = new StringBuilder(3);
                StringBuilder Folk = new StringBuilder(10);
                StringBuilder BirthDay = new StringBuilder(9);
                StringBuilder Code = new StringBuilder(19);
                StringBuilder Address = new StringBuilder(71);
                StringBuilder Agency = new StringBuilder(31);
                StringBuilder ExpireStart = new StringBuilder(9);
                StringBuilder ExpireEnd = new StringBuilder(9);
                byte[] pMsg = new byte[256];
                int len = 0;
                string[] temp;
                string[] baseinfo = new string[9];
                char[] param = { '\0' };

                //打开端口
                int intOpenRet = InitComm(1001);
                if (intOpenRet != 1)
                {
                    MessageBox.Show("阅读机具未连接");
                    return;
                }

                //卡认证
                int intReadRet = Authenticate();
                if (intReadRet != 1)
                {
                    //MessageBox.Show("卡认证失败");
                    CloseComm();
                    return;
                }
                //三种方式读取基本信息
                //ReadBaseInfos(推荐使用)

                int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
                if (intReadBaseInfosRet != 1)
                {
                    //MessageBox.Show("读卡失败");
                    CloseComm();
                    return;
                }

                label4.Text = Code.ToString();

                label6.Text = Name.ToString();

                label8.Text = Gender.ToString();

                label10.Text = Folk.ToString();

                label12.Text = BirthDay.ToString();

                label14.Text = Address.ToString();

                label16.Text = Agency.ToString();

                label18.Text = ExpireStart.ToString();

                label20.Text = ExpireEnd.ToString();

                pictureBox1.Load("photo.bmp");
                pictureBox1.Refresh();
                byte[] pMsg1 = new byte[256];

                int aa = ReadIINSNDN(pMsg1);
                //int aa = ReadIINSNDN(pMsg1);

                //ReadBaseMsg(不推荐使用)

                //intReadBaseInfosRet = ReadBaseMsg(pMsg, ref len);
                //if (intReadBaseInfosRet != 1)
                if (aa != 1)
                {
                    //MessageBox.Show("读卡失败");
                    CloseComm();
                    return;
                }
                temp = System.Text.Encoding.Default.GetString(pMsg1).Split(param);
                listBox1.Items.Add(temp);
                listBox1.Items.Add("-------------");
                int i = 0;
                foreach (string s in temp)
                {
                    if (s.Length == 0)
                        continue;
                    baseinfo[i  ] = s;
                }
                listBox1.Items.Add(temp.ToString());
                listBox1.Items.Add(baseinfo[0]);

                //关闭端口
                //int intCloseRet = CloseComm();
            }
            catch (Exception ex)
            {
                int intCloseRet = CloseComm();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Left -= 2;//设置label1左边缘与其容器的工作区左边缘之间的距离  
            if (label1.Right < 0)//当label1右边缘与其容器的工作区左边缘之间的距离小于0时      
            {
                label1.Left = this.Width;//设置label1左边缘与其容器的工作区左边缘之间的距离为该窗体的宽度
            }
        }

    }
     
}