【问题标题】:Update Windows OS System time on time.windows.com server by code通过代码在 time.windows.com 服务器上更新 Windows OS 系统时间
【发布时间】:2021-06-27 14:26:19
【问题描述】:

在 Windows 7 上,我习惯于自己管理 Windows 防火墙。

我通常会阻止所有 IN & OUT 连接规则,除非我需要。

但我希望操作系统也保持同步。

要做到这一点,我知道有一些方法,但我想通过代码 (C#) 来做到这一点。

我运行了一小段代码:

  1. 搜索/创建临时打开 UDP 端口 123 的 Windows 防火墙规则

  2. 启用“操作系统时间重新同步防火墙规则”

  3. 重新同步操作系统 Windows 系统时间

  4. 检查重新同步是否正确执行

  5. 禁用“操作系统时间重新同步防火墙规则”

我创建和修改 Windows 防火墙规则的部分运行良好,但当我尝试更新操作系统时间时就不一样了。

代码:

Public Declare Function W32TimeSyncNow Lib "w32time.dll" (ByVal computername As String, ByVal wait As Boolean, ByVal flag As UInteger) As UInteger

我在这里找到Windows API to trigger the time synchronization

好像不行。

我也想了解是否有办法获取 w32time 请求的状态。

这里是完整的代码。

public class WFM {

private string ruleName = "resync Windows OS";

[DllImport("w32time.dll")]
public static extern System.UInt32 W32TimeSyncNow(string computername, bool wait, System.UInt32 flag);

private void WFM_Load(object sender, EventArgs e) {
    this.resyncOs();
}

private void resyncOs() {
    try {
        Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
        INetFwPolicy2 fwPolicy2 = ((INetFwPolicy2)(Activator.CreateInstance(tNetFwPolicy2)));
        object currentProfiles = fwPolicy2.CurrentProfileTypes;
        INetFwRule2 resyncRule = null;
        foreach (INetFwRule2 rule in fwPolicy2.Rules) {
            if ((rule.Name == ruleName)) {
                resyncRule = rule;
            }
            
        }
        
        if ((resyncRule == null)) {
            resyncRule = this.createResyncRule();
            INetFwPolicy2 firewallPolicy = ((INetFwPolicy2)(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"))));
            firewallPolicy.Rules.Add(resyncRule);
        }
        
        // enable rule
        resyncRule.Enabled = true;
        WFM.W32TimeSyncNow(Environment.MachineName, true, 8);
        resyncRule.Enabled = false;
    }
    catch (Exception ex) {
        bool debug = true;
    }
    
}

private INetFwRule2 createResyncRule() {
    INetFwRule2 firewallRule = ((INetFwRule2)(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"))));
    firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
    // **General
    firewallRule.Name = ruleName;
    // name
    firewallRule.Description = "Enable UDP port 123 to resync Windows OS System time";
    firewallRule.Enabled = false;
    firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
    // operation
    // **Programs and Services
    // firewallRule.ApplicationName = ""                               'program
    // firewallRule.serviceName = ""                                   'service
    // **Remote Computers
    // **Protocols and e Ports
    firewallRule.Protocol = int.Parse(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
    firewallRule.LocalPorts = "123";
    firewallRule.RemotePorts = "*";
    firewallRule.Profiles = (((int)(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE)) || ((int)(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN)));
    return firewallRule;
}

}

附:注意。为了能够创建 Windows 防火墙规则,它需要允许以管理员身份执行代码,因此需要修改 app.manifest 替换行

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

感谢任何帮助。 谢谢。

【问题讨论】:

  • “好像不行。”什么不起作用?是“打开防火墙”,还是“从 time.windows.com 读取时间”,还是“别的什么”?
  • 您是否在此处检查了代码@codeproject.com:An SNTP Client for C# and VB.NET
  • @Luuk 非常感谢,也许你在我的帖子中错过了这个:'...我创建和修改 Windows 防火墙规则的部分运行良好,但当我尝试更新操作系统时不一样时间...'
  • @Luuk 我会检查你的链接,然后更新帖子。无论如何,非常感谢您的帮助

标签: c#


【解决方案1】:

好的,经过一番测试,我找到了解决方案。

只是改变

public static extern System.UInt32 W32TimeSyncNow(string computername, bool wait, System.UInt32 flag);

public static extern uint W32TimeSyncNow([MarshalAs(UnmanagedType.LPWStr)]String computername, bool wait, uint flag);

我仍然错过了第 5) 点,我想检查重新同步是否执行良好。

注意。已在 OS 中设置了对 Ntp Server 的同步请求数据(在注册表项中:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Parameters\NtpServer”, 在我的情况下,其值为“time.windows.com0x9”)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2016-06-14
    • 2019-11-25
    • 2019-09-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多