如何在Windows 10 IoT中设置系统时间?

时间:2015-06-02 01:28:31

标签: c# raspberry-pi windows-10-iot-core

有没有办法在我的应用程序中运行在Windows 10 IoT核心内幕预览版中的Raspberry Pi 2上的系统时间?

由于缺少kernel32.dll

,这不起作用
    [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
    extern static bool Win32SetSystemTime(ref SystemTime sysTime);

8 个答案:

答案 0 :(得分:11)

首先,connect to your Pi 2 using PowerShell

使用命令set-date设置时间。例如,如果要将日期设置为2015年10月3日星期六下午2:00,则可以键入set-date 10/3/2015 2:00PM

命令tzutil设置时区。输入tzutil /?作为使用

答案 1 :(得分:4)

您可以在UWP / IoT-Core系统上调用C#中的任何PowerShell例程。因此,PowerShell命令始终可用于您的应用程序。

有关示例,请参阅ProcessLauncher sample on GitHub

ALTERNATIVE,按如下方式安排启动PS脚本:

在主板上以管理员身份运行PowerShell(按Windows按钮,然后开始键入PowerShell,右键单击图标并选择“以管理员身份运行”)。

Set-ExecutionPolicy RemoteSigned

以管理员身份对RPi进行操作,并且:

setx PATH"%PATH%; C:\ Windows \ System32"

schtasks / create / tn" StartupPowerShell" / tr c:\ Startup.bat / sc onstart / ru SYSTEM

的startup.bat

powershell -command "C:\IotCoreStartup.ps1"

IotCoreStartup.ps

$logFile = 'C:\StartupLog.txt'

get-date > $logFile

tzutil /s "UTC" >> $logFile

# set alternate time servers
"Setting additional time servers" >> $logFile
w32tm /config /syncfromflags:manual /manualpeerlist:"0.windows.time.com 1.pool.ntp.org" >> $logFile

如果需要,删除计划任务:

schtasks / Delete / TN" StartupPowerShell"

如果要测试,则运行计划任务:

schtasks / Run / tn" StartupPowerShell"

答案 2 :(得分:4)

1-新通用项目
2-Add Reference> Extensions> Windows IOT Extensions for UWP
3-在MainPage.xaml上放置一个按钮,一个datepicker和一个timepicer控件 和

private void buttonSetSystemDatetime_Click(object sender, RoutedEventArgs e)
        {
            DateTimeOffset dto = datePicker1.Date+ timePicker1.Time;
            Windows.System.DateTimeSettings.SetSystemDateTime(dto);
        }

4- poject settings>设置目标版本和最低版本10.0;建造16299
5-双击appxmanifest>能力>检查“系统管理”

6-在物联网中运行app并按下按钮。而不是返回默认应用程序。瞧!系统日期时间已更改。

注意:而不是每次都设置它。我给你买一个便宜的rtc(实时时钟)模块。 (还需要额外的编码)

答案 3 :(得分:3)

看起来到目前为止似乎无法实际编辑系统时间,但这是一个我想出的工作,以便至少在您的应用中获得正确的时间。 我创建了一个TimeManager类,重要的部分如下。

获取您想要的正确时间(例如NTP,其他网络时间,用户输入等)并输入到UpdateOffset方法中。

在App的其余部分使用TimeManager.Now而不是DateTime.Now

    static TimeSpan _offset = new TimeSpan(0,0,0);
    public static TimeSpan CurrentOffset //Doesn't have to be public, it is for me because I'm presenting it on the UI for my information
    {
        get { return _offset; }
        private set { _offset = value; }
    }

    public static DateTime Now
    {
        get
        {
            return DateTime.Now - CurrentOffset;
        }
    }

    static void UpdateOffset(DateTime currentCorrectTime) //May need to be public if you're getting the correct time outside of this class
    {
        CurrentOffset = DateTime.UtcNow - currentCorrectTime;
        //Note that I'm getting network time which is in UTC, if you're getting local time use DateTime.Now instead of DateTime.UtcNow. 
    }

我还建议添加诸如跟踪上次更新时间之类的内容,并标记以指示时间是否已更新,只是不想混淆代码示例。

答案 4 :(得分:3)

我使用RTC在Raspberry Pi上设置时间。虽然Windows-iot没有本机支持在几个小时的考虑选项之后从实时时钟初始化Raspberry Pi的软件时钟,但我找到了适合我的东西。

我制作了一个控制台程序,可以将系统时间保存到RTC或读取RTC中的时间并将其打印为字符串。我制作了一个power shell脚本,它将在系统启动时运行该程序,以从实时时钟获取时间并将字符串传递给set-date命令。

详细信息如下:http://www.codeproject.com/Articles/1113626/Adding-the-Missing-Real-Time-Clock-to-Windows-IoT

答案 5 :(得分:2)

使用Renci SSH shell以管理员身份重新登录设备,然后使用powershell命令设置日期和时间。

public static int SshCommand(string command, out string dataOut, out string error)
    {

        dataOut = "";
        error = "";

        try
        {

            using (var client = new SshClient("127.0.0.1", USER_NAME, PASSWORD))
            {
                client.Connect();
                //command = "powershell -Command " + "\u0022" + "set-date -date '8/10/2017 8:30:00 AM'" + "\u0022";
                //command = "netsh interface ip show config";
                var cmd = client.RunCommand(command);
                var output = cmd.Result;
                client.Disconnect();
            }

            return 1;
        }
        catch (Exception ex)
        {
            error = ex.Message;
            return 0;
        }

    }


public static int SSHSetDateTime(DateTime dateTime)
    {

        // Variables
        int returnValue = 0;
        string command;
        string error;
        string dataOut;

        // Build date
        command = String.Format("powershell -Command \u0022set-date -date '{0:M/d/yyyy H:mm:ss tt}'\u0022", dateTime);

        //Build date
        if (SystemEx.SshCommand(command, out dataOut, out error) == 1)
        {
            // Ok
            returnValue = 1;
        }

        // Return
        return returnValue;

    }

答案 6 :(得分:1)

我意识到您正在询问如何以编程方式执行此操作,但是,以下内容应提供足够的信息来创建在启动时运行的PS脚本。

通过Powershell远程访问Raspberry Pi

1。)在开发PC上运行Windows 10 IoT核心观察器实用程序(C:\ Program Files(x86)\ Microsoft IoT \ WindowsIoTCoreWatcher.exe),右键单击检测到的设备,复制Raspberry Pi IP地址并选择复制IP地址。

◦单击窗口“开始”按钮

◦键入“WindowsIoTCoreWatcher”以将其提取到搜索结果中

◦您可能需要右键单击程序名称并选择“Pin to Start”将其固定到开始屏幕以便于访问

◦按Enter键运行

◦您的设备应在5秒左右出现在列表中。如果没有,请关闭Windows 10 IoT核心观察器,然后重新启动它

Windows IoT Core Watcher

2.。)在本地PC上启动管理员PowerShell控制台。最简单的方法是在Windows开始菜单附近的搜索Web和Windows文本框中键入“powershell”。 Windows将在您的计算机上找到PowerShell。右键单击Windows PowerShell条目,然后选择以管理员身份运行。 PS控制台将显示。

Running Powershell as Administrator

3.)您可能需要在桌面上启动WinRM服务以启用远程连接。在PS控制台中,键入以下命令:

 net start WinRM 

4.。)在PS控制台中,键入以下命令,将''替换为在prev中复制的IP值:

 Set-Item WSMan:\localhost\Client\TrustedHosts -Value <machine-name or IP Address>

5.键入Y并按Enter确认更改。

6.现在您可以开始与Windows IoT核心设备进行会话。从管理员PS控制台,键入:

Enter-PSSession -ComputerName <IP Address> -Credential localhost\Administrator

7.在凭证对话框中输入以下默认密码:

  

点@ ssw0rd

注意:连接过程不是立即进行的,最多可能需要30秒。

如果您已成功连接到设备,则应在提示之前看到设备的IP地址。

Connected to the Raspberry using PS

重命名设备并设置日期和时间

1.要更改计算机名称,请使用setcomputername实用程序。在PowerShell中,键入以下命令。

  

setcomputername

2.对于用于在实验室稍后发布到Azure的安全令牌,Pi上的日期和时间必须正确才有效。要检查Pi上的当前时区设置,请键入:

  

tzutil / g

3.如果报告的时区不正确,您可以使用(您可能需要在PowerShell窗口上增加缓冲区大小)找到有效时区列表:

  

tzutil / l

4.要设置时区,请从上面的步骤中找到所需时区的ID,然后使用:

  

tzutil / s“你的时区名称”

例如,“太平洋标准时间”

  

tzutil / s“太平洋标准时间”

5.要检查Raspberry Pi上的日期,请键入

  

GET-日期

6.如果日期或时间不正确,请使用设置日期实用程序

  

设定日期“mm / dd / yy hh:mm:ss AM / PM”

例如,如果是2016年1月3日下午12:15:

  

设定日期“01/03/16 12:15 PM”

7.重新启动设备以使更改生效。您可以按如下方式使用shutdown命令:

  

shutdown / r / t 0

答案 7 :(得分:1)

参考Microsoft API参考文档,在 Windows.System 命名空间中,您可以使用 SetSystemDateTime 方法设置系统日期和时间。

但你必须知道它可以在

中找到
  • Windows IoT Extension SDK(介绍v10.0.16225.0)及以上

您可以使用DateTimeSettings静态类

public static class DateTimeSettings

然后调用SetSystemDateTime静态方法并发送DateTimeOffset类型的对象,以便在Windows上设置日期和时间。

public static void SetSystemDateTime(DateTimeOffset utcDateTime)

https://docs.microsoft.com/en-us/uwp/api/windows.system.datetimesettings

相关问题