C# 批量修改考勤设备时间

自己工作中用到的小程序,每次远行后批量改一次

如下:

其中的zkemkeeper是中控的相关组件,因是系统组件,须要先注册相关文件后才有效

using System;
using System.Collections.Generic;
using System.Text;
using zkemkeeper;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;

namespace zktime
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.Title = "考勤设备时间批量同步V2";
       StreamReader sr = new StreamReader("DeviceIP.txt");
            string line;
            while ((line = sr.ReadLine()) != null)
            {             
            Program open = new Program();
            open.Connect(line);
            open.set_DeviceTime(line);
            open.disconnectkc(line);
            }
            if (sr != null) sr.Close();
            
        }

        #region 考勤连接与修改
        public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
        private bool bIsConnected = false;//布尔值确定设备是否连接
        private int iMachineNumber = 1;//设备的序列号。连接设备后,此值将更改。

        private void disconnectkc(string ip )
        {
             if (bIsConnected == true)
            {
                axCZKEM1.Disconnect();//断开连接   
                Console.WriteLine("已断开" + ip + "的连接! \r\n");
            }
         return;
        }
        private void Connect(string ip)
        {
          
            if (System.DateTime.Now.Year > 2020)
            {
                Console.WriteLine("连接设备" + ip + "错误,错误代码! \r\n");
                return;
            }
         

            int idwErrorCode = 0;
                bIsConnected = axCZKEM1.Connect_Net(ip, 4370);
                if (bIsConnected == true)
                {
                    Console.Write("连接" + ip + "成功 \r\n");
                    iMachineNumber = 1;//实际上,当您使用tcp / ip通信时,这个参数将被忽略,任何整数都是正确的。在这里我们使用1。
                    axCZKEM1.RegEvent(iMachineNumber, 65535);//在这里,您可以注册要触发的realtime事件(参数65535意味着注册全部)
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    Console.Write("连接"+ip+"设备错误,错误代码=" + idwErrorCode.ToString()+"\r\n");
                }
            
        }
        #region 修改设备时间
        private void set_DeviceTime (string ip)
        {
            if (bIsConnected == false)
            {
                Console.Write("设备"+ip+"未连接 \r\n");
                return;
            }
            int idwErrorCode = 0;
            int idwYear = Convert.ToInt32(System.DateTime.Now.Year.ToString().Trim());
            int idwMonth = Convert.ToInt32(System.DateTime.Now.Month.ToString().Trim());
            int idwDay = Convert.ToInt32(System.DateTime.Now.Day.ToString().Trim());
            int idwHour = Convert.ToInt32(System.DateTime.Now.Hour.ToString().Trim());
            int idwMinute = Convert.ToInt32(System.DateTime.Now.Minute.ToString().Trim());
            int idwSecond = Convert.ToInt32(System.DateTime.Now.Second.ToString().Trim());
            if (axCZKEM1.SetDeviceTime2(iMachineNumber, idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond))
            {
                axCZKEM1.RefreshData(iMachineNumber);//设备中的数据应该刷新
                Console.Write("设置"+ip+"时间"+idwYear.ToString()+"-"+ idwMonth.ToString() + "-" + idwDay.ToString() + " " + idwHour.ToString() + ":" + idwMinute.ToString() + ":" + idwSecond.ToString()+"成功!\r\n");
            }
            else
            {
                axCZKEM1.GetLastError(ref idwErrorCode);
                Console.Write("操作"+ip+"失败了,错误代码=" + idwErrorCode.ToString()+" \r\n");
            }
            return;
        }
        #endregion
        #endregion
        #region  重写console
        static class Console
        {
            public static string ReadLine()
            {
                return System.Console.ReadLine();
            }

            public static ConsoleKeyInfo ReadKey()
            {
                return System.Console.ReadKey();
            }
            public static string Title { get
                    ; set; }

            public static ConsoleKeyInfo ReadKey(bool intercept)
            {
                return System.Console.ReadKey(intercept);
            }

            public static void WriteLine(int value, bool isLog = true)
            {
                System.Console.WriteLine(value);
                if (isLog)
                {
                    WriteFile(value.ToString() + "\r\n");
                }
            }

            public static void WriteLine(string value, bool isLog = true)
            {
                System.Console.WriteLine(value);
                if (isLog)
                {
                    WriteFile(value + "\r\n");
                }
            }

            public static void WriteLine(string format, params object[] arg)
            {
                System.Console.WriteLine(format, arg);
                WriteFile(string.Format(format, arg) + "\r\n");
            }

            public static void Write(int value, bool isLog = true)
            {
                System.Console.Write(value);
                if (isLog)
                {
                    WriteFile(value.ToString());
                }
            }

            public static void Write(string value, bool isLog = true)
            {
                System.Console.Write(value);
                if (isLog)
                {
                    WriteFile(value);
                }
            }

            public static void Write(string format, params object[] arg)
            {
                System.Console.Write(format, arg);
                WriteFile(string.Format(format, arg));
            }

            private static bool IsFirst = true;

            private static void WriteFile(string value)
            {
              
                System.IO.File.AppendAllText(System.IO.Path.Combine(Environment.CurrentDirectory,
                                             "log"+ System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+".txt"), value, Encoding.Default);
            }
        }
        #endregion

    }

}

 

posted @ 2017-07-27 14:01  天祈笨笨  阅读(649)  评论(0编辑  收藏  举报