资源简介
自己写的一个简单获取北京时间的程序,分享一下,若有不足,请多多指正。

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication28
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
}
public static DateTime GetNetworkTime()
{
//default Windows time server
const string ntpServer = “time.windows.com“;
// NTP message size - 16 bytes of the digest (RFC 2030)
var ntpData = new byte[48];
//Setting the Leap Indicator Version Number and Mode values
ntpData[0] = 0x1B; //LI = 0 (no warning) VN = 3 (IPv4 only) Mode = 3 (Client Mode)
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
//The UDP port number assigned to NTP is 123
var ipEndPoint = new IPEndPoint(addresses[0] 123);
//NTP uses UDP
var socket = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
socket.Connect(ipEndPoint);
//Stops code hang if NTP is blocked
socket.ReceiveTimeout = 3000;
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
//Offset to get to the “Transmit Timestamp“ field (time at which the reply
//departed the server for the client in 64-bit timestamp format.“
const byte serverReplyTime = 40;
//Get the seconds part
ulong intPart = BitConverter.ToUInt32(ntpData serverReplyTime);
//Get the seconds fraction
ulong fractPart = BitConverter.ToUInt32(ntpData serverReplyTime + 4);
//Convert From big-endian to little-endian
intPart = SwapEndianness(intPart);
fractPart = SwapEndianness(fractPart);
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
//**UTC** time
var networkDateTime = (new DateTime(1900 1 1 0 0 0 DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
return networkDateTime.ToLocalTime();
}
// stackoverflow.com/a/3294698/162671
static uint SwapEndianness(ulong x)
{
return (uint)(((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
private void timer1_Tick(object sender EventArgs e)
{
GetNetworkTime();
label1.Text = GetNetworkT
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-04-04 22:05 标准时间\
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\
文件 187 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\App.config
文件 2992 2015-04-04 22:01 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.Designer.cs
文件 3068 2015-04-04 22:01 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.cs
文件 6011 2015-04-04 22:01 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Form1.resx
文件 537 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Program.cs
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\
文件 1374 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\AssemblyInfo.cs
文件 2902 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Resources.Designer.cs
文件 5612 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Resources.resx
文件 1112 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Settings.Designer.cs
文件 249 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\Properties\Settings.settings
文件 3830 2015-04-04 21:21 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\WindowsFormsApplication28.csproj
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\
文件 9728 2015-04-04 22:01 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.exe
文件 187 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.exe.config
文件 26112 2015-04-04 22:01 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.pdb
文件 23168 2015-04-04 22:04 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe
文件 187 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe.config
文件 490 2013-06-18 20:28 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\bin\Debug\WindowsFormsApplication28.vshost.exe.manifest
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\
目录 0 2015-04-04 22:05 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\
文件 865 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7136 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
目录 0 2015-04-07 15:10 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TempPE\
文件 0 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2015-04-04 21:20 标准时间\WindowsFormsApplication28\WindowsFormsApplication28\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略9个文件信息
相关资源
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
- C#指纹识别系统程序 报告
- c# 高校档案信息管理系统
- c#向word文件插入图片
- C#左侧导航菜单(动态生成)
评论
共有 条评论