C#远程时间同步助手软件设计

来源:互联网 发布:淘宝卡怎么办理 编辑:程序博客网 时间:2024/04/29 05:29

C#远程时间同步助手软件设计

本程序才C#语言开发,实现远程时间同步功能,可以将本地时间每隔一段时间与时间服务器时间进行同步!不足之处还望见谅!

软件开发环境:Visual Studio 2010

软件安装简单说明

时间服务器的ntp服务开启状态:

TimeSync.exe时间同步软件
本软件为纯绿色版,不需要安装,可以直接点击运行,编译环境.net4.0

第一步:双击TimeSync.exe
第二步:参数设置--》设置--》输入服务器ip地址和端口以及间隔同步时间---》确定---》重新启动
第三步:参数设置--》设置为自启动(开机自动开启)

一、Win7开启NTP服务

二、程序运行结果展示

三、部分程序代码展示

四、程序源代码下载地址


一、Win7开启NTP服务

在桌面上右击“计算机--》选择“管理--》然后选择--》服务

选中“Windows Time”,设置为开启,这样就可以将“Windows Time”这一个服务打开。“开始”--》“运行”--》输入“regedit”打开注册表。

找到NetSerVer这一项,具体见插图,里面很详细。


将NetSerVer下Enabled的值设置为 1。

重启电脑系统,就可以使用该电脑作为NTP服务器了

 



二、程序运行结果展示

1.程序主窗体


2.程序服务器配置


3.程序托盘设计


4.程序所用控件以及布局等等


三、部分程序代码展示

//读取配置信息        public void Read()        {            IniFile cfg = new IniFile(Application.StartupPath + @"\Config\SetConfig.ini");            if (cfg != null)            {                NtpServerIP = cfg.IniReadValue("ServerConfig", "IP");                comboBox1.Items.Add(NtpServerIP);                comboBox1.SelectedIndex = 0;                NtpServerPort = cfg.IniReadValue("ServerConfig", "Port");                textBox3.Text = cfg.IniReadValue("ServerConfig", "Interval");            }        }        private void button1_Click(object sender, EventArgs e)        {            textBox1.Clear();            var dt =  getTime();            if (dt.ToString("yyyy-MM-dd HH:mm:ss") == "1900-01-01 08:00:00")            {                 MessageBox.Show("服务器异常!", "提示");             }            else            {                var Dt = DateTime.Now;                label5.Text = (Dt - dt).ToString("ss");                textBox1.Text = dt.ToString("yyyy-MM-dd HH:mm:ss");            }        }        private void btnStartSet_Click(object sender, EventArgs e)        {            if (SetTime())            { MessageBox.Show("时间同步成功!", "提示"); }        }        public DateTime getTime()        {            var client = new SNTPTimeClient.SNTPTimeClient(NtpServerIP, NtpServerPort);            if (client.Connect())            {                DateTime getEd = client.ReceiveTimestamp;//获取指定IP的系统时间                return getEd;            }            else            {                string date = "1900-01-01 08:00:00";                DateTime getEd = Convert.ToDateTime(date);                return getEd;            }           // SNTPTimeClient.SNTPTimeClient.SetLocalTime(ref st);//设置本地时间          }        public bool SetTime()        {            var client = new SNTPTimeClient.SNTPTimeClient(NtpServerIP, NtpServerPort);            if (client.Connect())            {                DateTime getEd = client.ReceiveTimestamp;//获取指定IP的系统时间                var st = new SystemTime                {                    wDay = (ushort)getEd.Day,                    wDayOfWeek = (ushort)getEd.DayOfWeek,                    wHour = (ushort)getEd.Hour,                    wMiliseconds = (ushort)getEd.Millisecond,                    wMinute = (ushort)getEd.Minute,                    wMonth = (ushort)getEd.Month,                    wSecond = (ushort)getEd.Second,                    wYear = (ushort)getEd.Year                };                SNTPTimeClient.SNTPTimeClient.SetLocalTime(ref st);//设置本地时间                //notifyIcon1.ShowBalloonTip(500, "提示", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ToolTipIcon.Info);                 toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");                toolStripStatusLabel3.Text = textBox3.Text.Trim() + "分钟后同步";                return true;            }            else            {                notifyIcon1.ShowBalloonTip(500, "提示","服务器异常", ToolTipIcon.Info);                return false;            }           }        //最小化系统托盘图标可见          private void FrmMain_SizeChanged(object sender, EventArgs e)          {              ShowInTaskbar = false;              notifyIcon1.Visible = true;              if (WindowState == FormWindowState.Minimized)              notifyIcon1.ShowBalloonTip(500,"提示","小飞时间同步程序正在运行",ToolTipIcon.Info);          }        //双击托盘图标          private void notifyIcon1_DoubleClick(object sender, EventArgs e)          {              if (WindowState != FormWindowState.Minimized) return;              Show();              WindowState = FormWindowState.Normal;              notifyIcon1.Visible = false;              ShowInTaskbar = true;          }        //拦截关闭按钮为最小化          private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)          {              e.Cancel = true;              WindowState = FormWindowState.Minimized;              notifyIcon1.ShowBalloonTip(500, "提示", "时间同步程序正在运行", ToolTipIcon.Info);          }          private void 显示主窗体ToolStripMenuItem_Click(object sender, EventArgs e)          {              WindowState = FormWindowState.Normal;          }          private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)          {              if (MessageBox.Show("是否退出程序?", "退出", MessageBoxButtons.OKCancel) != DialogResult.OK) return;              Dispose();              Close();          }          private void timerLocal_Tick(object sender, EventArgs e)          {              textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");              //var sec = STimer / 1000;              //var dt = DateTime.Parse(Dt);              //toolStripStatusLabel2.Text = dt.AddSeconds(sec).ToString("yyyy-MM-dd HH:mm:ss");                    }          private void timerNet_Tick(object sender, EventArgs e)          {              SetTime();          }          private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)          {              退出ToolStripMenuItem_Click(sender,e);          }          private void 服务器参数ToolStripMenuItem_Click(object sender, EventArgs e)          {              FrmSeting frmseting = new FrmSeting();              frmseting.Show();          }        //自启动          public static bool SetSelfStart()          {              try              {                  var exeDir = Application.ExecutablePath;                  var rk = Registry.LocalMachine;                  var softWare = rk.OpenSubKey("SOFTWARE");                  var microsoft = softWare.OpenSubKey("Microsoft");                  var windows = microsoft.OpenSubKey("Windows");                  var current = windows.OpenSubKey("CurrentVersion");                  var run = current.OpenSubKey(@"Run", true);                  run.SetValue("时间同步程序", exeDir);                  return true;              }              catch (Exception ex)              {                  MessageBox.Show(ex.Message);                  return false;              }          }        //取消自启动          public static bool CancelSelfStart()          {              try              {                  var rk = Registry.LocalMachine;                  var softWare = rk.OpenSubKey("SOFTWARE");                  var microsoft = softWare.OpenSubKey("Microsoft");                  var windows = microsoft.OpenSubKey("Windows");                  var current = windows.OpenSubKey("CurrentVersion");                  var run = current.OpenSubKey(@"Run", true);                  run.DeleteValue("时间同步程序");                  return true;              }              catch (Exception ex)              {                  MessageBox.Show(ex.Message);                  return false;              }          }          private void 设置为自启动ToolStripMenuItem1_Click(object sender, EventArgs e)          {              if (SetSelfStart())              {                  MessageBox.Show("加入自启动成功", "提示", MessageBoxButtons.OK);              }          }          private void 取消自启动ToolStripMenuItem1_Click(object sender, EventArgs e)          {              if (CancelSelfStart())              {                  MessageBox.Show("成功取消自启动", "提示", MessageBoxButtons.OK);              }          }        


四、程序源代码下载地址

程序工程下载地址  http://download.csdn.net/detail/coderjyf/9545672


0 0
原创粉丝点击