123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using SNTPTimeClient;
- using INIFile;
- using Microsoft.Win32;
- //http://www.codesc.net
- namespace TimeSync
- {
- public partial class FrmMain : Form
- {
- public static string NtpServerIP = "192.168.0.189";
- public static string NtpServerPort = "123";
- public static int STimer = 10;//时间间隔分钟
- public string Dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- public FrmMain()
- {
- InitializeComponent();
- Starting();
- SetTime();
- }
- public void Starting()
- {
- Read();
- timerLocal.Enabled = true;
- timerNet.Interval = int.Parse(textBox3.Text.Trim()) * 60 * 1000;
- timerNet.Enabled = true;
- }
- //读取配置信息
- public void Read()
- {
- IniFile cfg = new IniFile(Application.StartupPath + @"\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);
- }
- }
-
- }
- }
|