using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; using Microsoft.Win32; namespace SNTP_server_interface { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void SetSNTP() { RegistryKey rkRoot = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer",true); rkRoot.SetValue("Enabled", 1, RegistryValueKind.DWord); rkRoot = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\W32Time\Config", true); rkRoot.SetValue("AnnounceFlags", 5, RegistryValueKind.DWord); System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\W32tm.exe", "net stop w32time && net start w32time"); System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\W32tm.exe", "/resync /rediscover"); } public void closeSNTP() { RegistryKey rkRoot = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer", true); rkRoot.SetValue("Enabled", 0, RegistryValueKind.DWord); System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\W32tm.exe", "net stop w32time && net start w32time"); } private void AddPort() { StringBuilder sBuilder = new StringBuilder(); sBuilder.AppendLine("@echo off"); sBuilder.AppendLine("netsh firewall set portopening protocol = UDP port = 123 name = SNTP_Server_Port:123 mode = ENABLE scope = SUBNET profile = CURRENT"); sBuilder.Append(":END"); Stream st = new FileStream(@"C:\test.bat", FileMode.OpenOrCreate); using (StreamWriter sw = new StreamWriter(st,Encoding.Default))//默认编码可以使中文字符不会是乱码[/color] { sw.Write(sBuilder.ToString()); sw.Close(); st.Dispose(); st.Close(); } Process ps = new Process(); ProcessStartInfo psi = new ProcessStartInfo(@"C:\test.bat"); psi.UseShellExecute = false; psi.CreateNoWindow = true; ps.StartInfo = psi; ps.Start(); while (ps.HasExited == false) { } System.IO.File.Delete(@"C:\port.bat"); } private void DeletePort() { StringBuilder sBuilder = new StringBuilder(); sBuilder.AppendLine("@echo off"); sBuilder.AppendLine("netsh firewall delete portopening protocol = UDP port = 123"); sBuilder.Append(":END"); Stream st = new FileStream(@"C:\ClosePort.bat", FileMode.OpenOrCreate); using (StreamWriter sw = new StreamWriter(st, Encoding.Default)) { sw.Write(sBuilder.ToString()); sw.Close(); st.Dispose(); st.Close(); } Process ps = new Process(); ProcessStartInfo psi = new ProcessStartInfo(@"C:\ClosePort.bat"); psi.UseShellExecute = false; psi.CreateNoWindow = true; ps.StartInfo = psi; ps.Start(); while (ps.HasExited == false) { } System.IO.File.Delete(@"C:\ClosePort.bat"); } private void butStart_Click(object sender, EventArgs e) { AddPort(); SetSNTP(); MessageBox.Show("The time server is running.\nIf you cannot synchronize the WinPAC system time with the PC, try to synchronize the PC system time using the internet time server."); } private void butClose_Click(object sender, EventArgs e) { DeletePort(); closeSNTP(); MessageBox.Show("The time server have be closed"); } private void label1_Click(object sender, EventArgs e) { } } }