FrmMain.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using SNTPTimeClient;
  10. using INIFile;
  11. using Microsoft.Win32;
  12. //http://www.codesc.net
  13. namespace TimeSync
  14. {
  15. public partial class FrmMain : Form
  16. {
  17. public static string NtpServerIP = "192.168.0.189";
  18. public static string NtpServerPort = "123";
  19. public static int STimer = 10;//时间间隔分钟
  20. public string Dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  21. public FrmMain()
  22. {
  23. InitializeComponent();
  24. Starting();
  25. SetTime();
  26. }
  27. public void Starting()
  28. {
  29. Read();
  30. timerLocal.Enabled = true;
  31. timerNet.Interval = int.Parse(textBox3.Text.Trim()) * 60 * 1000;
  32. timerNet.Enabled = true;
  33. }
  34. //读取配置信息
  35. public void Read()
  36. {
  37. IniFile cfg = new IniFile(Application.StartupPath + @"\SetConfig.ini");
  38. if (cfg != null)
  39. {
  40. NtpServerIP = cfg.IniReadValue("ServerConfig", "IP");
  41. comboBox1.Items.Add(NtpServerIP);
  42. comboBox1.SelectedIndex = 0;
  43. NtpServerPort = cfg.IniReadValue("ServerConfig", "Port");
  44. textBox3.Text = cfg.IniReadValue("ServerConfig", "Interval");
  45. }
  46. }
  47. private void button1_Click(object sender, EventArgs e)
  48. {
  49. textBox1.Clear();
  50. var dt = getTime();
  51. if (dt.ToString("yyyy-MM-dd HH:mm:ss") == "1900-01-01 08:00:00")
  52. {
  53. MessageBox.Show("服务器异常!", "提示");
  54. }
  55. else
  56. {
  57. var Dt = DateTime.Now;
  58. label5.Text = (Dt - dt).ToString("ss");
  59. textBox1.Text = dt.ToString("yyyy-MM-dd HH:mm:ss");
  60. }
  61. }
  62. private void btnStartSet_Click(object sender, EventArgs e)
  63. {
  64. if (SetTime())
  65. { MessageBox.Show("时间同步成功!", "提示"); }
  66. }
  67. public DateTime getTime()
  68. {
  69. var client = new SNTPTimeClient.SNTPTimeClient(NtpServerIP, NtpServerPort);
  70. if (client.Connect())
  71. {
  72. DateTime getEd = client.ReceiveTimestamp;//获取指定IP的系统时间
  73. return getEd;
  74. }
  75. else
  76. {
  77. string date = "1900-01-01 08:00:00";
  78. DateTime getEd = Convert.ToDateTime(date);
  79. return getEd;
  80. }
  81. // SNTPTimeClient.SNTPTimeClient.SetLocalTime(ref st);//设置本地时间
  82. }
  83. public bool SetTime()
  84. {
  85. var client = new SNTPTimeClient.SNTPTimeClient(NtpServerIP, NtpServerPort);
  86. if (client.Connect())
  87. {
  88. DateTime getEd = client.ReceiveTimestamp;//获取指定IP的系统时间
  89. var st = new SystemTime
  90. {
  91. wDay = (ushort)getEd.Day,
  92. wDayOfWeek = (ushort)getEd.DayOfWeek,
  93. wHour = (ushort)getEd.Hour,
  94. wMiliseconds = (ushort)getEd.Millisecond,
  95. wMinute = (ushort)getEd.Minute,
  96. wMonth = (ushort)getEd.Month,
  97. wSecond = (ushort)getEd.Second,
  98. wYear = (ushort)getEd.Year
  99. };
  100. SNTPTimeClient.SNTPTimeClient.SetLocalTime(ref st);//设置本地时间
  101. //notifyIcon1.ShowBalloonTip(500, "提示", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ToolTipIcon.Info);
  102. toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  103. toolStripStatusLabel3.Text = textBox3.Text.Trim() + "分钟后同步";
  104. return true;
  105. }
  106. else
  107. {
  108. notifyIcon1.ShowBalloonTip(500, "提示","服务器异常", ToolTipIcon.Info);
  109. return false;
  110. }
  111. }
  112. //最小化系统托盘图标可见
  113. private void FrmMain_SizeChanged(object sender, EventArgs e)
  114. {
  115. ShowInTaskbar = false;
  116. notifyIcon1.Visible = true;
  117. if (WindowState == FormWindowState.Minimized)
  118. notifyIcon1.ShowBalloonTip(500,"提示","小飞时间同步程序正在运行",ToolTipIcon.Info);
  119. }
  120. //双击托盘图标
  121. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  122. {
  123. if (WindowState != FormWindowState.Minimized) return;
  124. Show();
  125. WindowState = FormWindowState.Normal;
  126. notifyIcon1.Visible = false;
  127. ShowInTaskbar = true;
  128. }
  129. //拦截关闭按钮为最小化
  130. private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
  131. {
  132. e.Cancel = true;
  133. WindowState = FormWindowState.Minimized;
  134. notifyIcon1.ShowBalloonTip(500, "提示", "时间同步程序正在运行", ToolTipIcon.Info);
  135. }
  136. private void 显示主窗体ToolStripMenuItem_Click(object sender, EventArgs e)
  137. {
  138. WindowState = FormWindowState.Normal;
  139. }
  140. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  141. {
  142. if (MessageBox.Show("是否退出程序?", "退出", MessageBoxButtons.OKCancel) != DialogResult.OK) return;
  143. Dispose();
  144. Close();
  145. }
  146. private void timerLocal_Tick(object sender, EventArgs e)
  147. {
  148. textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  149. //var sec = STimer / 1000;
  150. //var dt = DateTime.Parse(Dt);
  151. //toolStripStatusLabel2.Text = dt.AddSeconds(sec).ToString("yyyy-MM-dd HH:mm:ss");
  152. }
  153. private void timerNet_Tick(object sender, EventArgs e)
  154. {
  155. SetTime();
  156. }
  157. private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
  158. {
  159. 退出ToolStripMenuItem_Click(sender,e);
  160. }
  161. private void 服务器参数ToolStripMenuItem_Click(object sender, EventArgs e)
  162. {
  163. FrmSeting frmseting = new FrmSeting();
  164. frmseting.Show();
  165. }
  166. //自启动
  167. public static bool SetSelfStart()
  168. {
  169. try
  170. {
  171. var exeDir = Application.ExecutablePath;
  172. var rk = Registry.LocalMachine;
  173. var softWare = rk.OpenSubKey("SOFTWARE");
  174. var microsoft = softWare.OpenSubKey("Microsoft");
  175. var windows = microsoft.OpenSubKey("Windows");
  176. var current = windows.OpenSubKey("CurrentVersion");
  177. var run = current.OpenSubKey(@"Run", true);
  178. run.SetValue("时间同步程序", exeDir);
  179. return true;
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageBox.Show(ex.Message);
  184. return false;
  185. }
  186. }
  187. //取消自启动
  188. public static bool CancelSelfStart()
  189. {
  190. try
  191. {
  192. var rk = Registry.LocalMachine;
  193. var softWare = rk.OpenSubKey("SOFTWARE");
  194. var microsoft = softWare.OpenSubKey("Microsoft");
  195. var windows = microsoft.OpenSubKey("Windows");
  196. var current = windows.OpenSubKey("CurrentVersion");
  197. var run = current.OpenSubKey(@"Run", true);
  198. run.DeleteValue("时间同步程序");
  199. return true;
  200. }
  201. catch (Exception ex)
  202. {
  203. MessageBox.Show(ex.Message);
  204. return false;
  205. }
  206. }
  207. private void 设置为自启动ToolStripMenuItem1_Click(object sender, EventArgs e)
  208. {
  209. if (SetSelfStart())
  210. {
  211. MessageBox.Show("加入自启动成功", "提示", MessageBoxButtons.OK);
  212. }
  213. }
  214. private void 取消自启动ToolStripMenuItem1_Click(object sender, EventArgs e)
  215. {
  216. if (CancelSelfStart())
  217. {
  218. MessageBox.Show("成功取消自启动", "提示", MessageBoxButtons.OK);
  219. }
  220. }
  221. }
  222. }