获取网络上的北京时间,如果大于设定的过期时间就...
设置一个过期时间 ,定义时间变量dd,dd获取网络上的北京时间,如果获取失败,就获取计算机当前时间,再比对dd与过期时间,如果dd大于过期时间,就执行...
// 比对固定时间,看是否超时。 private void CompareTime() { try { dd = GetBeijingTime(); i = DateTime.Compare(dd, Convert.ToDateTime("2013-1-1 00:00:00")); if (i < 0) { dd = DateTime.Today; } } catch { } i = DateTime.Compare(dd, Convert.ToDateTime("2014-5-5 00:00:00")); if (i > 0) { btnOpen.Enabled = false; } } // 获取北京时间 public static DateTime GetBeijingTime() { //t0 = new Date().getTime(); //nyear = 2011; //nmonth = 7; //nday = 5; //nwday = 2; //nhrs = 17; //nmin = 12; //nsec = 12; DateTime dt; WebRequest wrt = null; WebResponse wrp = null; try { wrt = WebRequest.Create("http://www.beijing-time.org/time.asp"); wrp = wrt.GetResponse(); string html = string.Empty; using (Stream stream = wrp.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) { html = sr.ReadToEnd(); } } string[] tempArray = html.Split(';'); for (int i = 0; i < tempArray.Length; i++) { tempArray[i] = tempArray[i].Replace("\r\n", ""); } string year = tempArray[1].Substring(tempArray[1].IndexOf("nyear=") + 6); string month = tempArray[2].Substring(tempArray[2].IndexOf("nmonth=") + 7); string day = tempArray[3].Substring(tempArray[3].IndexOf("nday=") + 5); string hour = tempArray[5].Substring(tempArray[5].IndexOf("nhrs=") + 5); string minite = tempArray[6].Substring(tempArray[6].IndexOf("nmin=") + 5); string second = tempArray[7].Substring(tempArray[7].IndexOf("nsec=") + 5); dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second); } catch (WebException) { return DateTime.Parse("2011-4-1"); } catch (Exception) { return DateTime.Parse("2011-1-1"); } finally { if (wrp != null) wrp.Close(); if (wrt != null) wrt.Abort(); } return dt; }