Ejemplo n.º 1
0
        private void btnSetByManulTime_Click(object sender, EventArgs e)
        {
            DateTime dtSet     = dtSet = dtpManualTime.Value;
            bool     isSuccess = NTPTimeHelper.UseDllSetLocalDateTime(dtSet);

            if (isSuccess == false)
            {
                MessageBox.Show("程序出错,设置失败");
            }
        }
Ejemplo n.º 2
0
 private void btnSetByNtpTime_Click(object sender, EventArgs e)
 {
     try
     {
         timComputer.Stop();
         Ping      ping   = new Ping();
         PingReply reply  = ping.Send("www.baidu.com");//百度IP
         Ping      ping2  = new Ping();
         PingReply reply2 = ping2.Send("www.aliyun.com");
         if (reply.Status == IPStatus.Success || reply2.Status == IPStatus.Success)
         {
             DateTime dateTimeOrigin = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
             string   errorMessage   = "";
             if (cmbHostList.Text != "")
             {
                 DateTime ntpDateTime = NTPTimeHelper.GetNtpDateTime(cmbHostList.Text, out errorMessage);
                 if (errorMessage == "" && ntpDateTime.Date != dateTimeOrigin)
                 {
                     bool isSuccess = NTPTimeHelper.UseDllSetLocalDateTime(ntpDateTime);
                     if (isSuccess == false)
                     {
                         MessageBox.Show("程序出错,设置失败");
                         return;
                     }
                     txtNtpTime.Text = ntpDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                     return;
                 }
             }
             for (int i = 0; i < ntpHostList.Length; i++)
             {
                 DateTime ntpDateTime = NTPTimeHelper.GetNtpDateTime(ntpHostList[i], out errorMessage);
                 if (errorMessage != "" || ntpDateTime.Date == dateTimeOrigin)
                 {
                     continue;
                 }
                 if (i == ntpHostList.Length)
                 {
                     MessageBox.Show("所有服务器均无法获取时间,请重试");
                     return;
                 }
                 bool isSuccess = NTPTimeHelper.UseDllSetLocalDateTime(ntpDateTime);
                 if (isSuccess == false)
                 {
                     MessageBox.Show("程序出错,设置失败");
                     return;
                 }
                 cmbHostList.Text = ntpHostList[i];
                 txtNtpTime.Text  = ntpDateTime.ToString("yyyy-MM-dd HH:mm:ss");
                 break;
             }
         }
         else
         {
             MessageBox.Show("网络连接失败!请检查电脑网络连接是否正常");
         }
     }catch (Exception ex)
     {
         MessageBox.Show("网络连接失败!请检查电脑网络连接是否正常");
     }
     finally
     {
         timComputer.Start();
     }
 }