C# 以管理员身份登录设备,然后使用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; }

C# 以管理员身份登录设备,然后使用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; },c#,raspberry-pi,windows-10-iot-core,C#,Raspberry Pi,Windows 10 Iot Core,请参阅Microsoft API参考文档,在Windows.System命名空间中,您可以使用SetSystemDateTime方法设置系统日期和时间 但你必须知道它在美国有售 Windows物联网扩展SDK(推出v10.0.16225.0)及以上版本 您可以使用DateTimeSettings静态类 public static class DateTimeSettings 然后调用SetSystemDateTime静态方法并发送DateTimeOffset类型的对象,以便在Windows

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

但你必须知道它在美国有售

  • Windows物联网扩展SDK(推出v10.0.16225.0)及以上版本
您可以使用DateTimeSettings静态类

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

public static void SetSystemDateTime(DateTimeOffset utcDateTime)

1-新的通用项目
2-为UWP添加参考>扩展>Windows物联网扩展
3-在主页上放置一个按钮、一个日期选择器和一个计时器控件。xaml

4-在项目设置>设置目标版本和最低版本10.0;构建16299
5-双击appxmanifest>功能>检查“系统管理”

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


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

我看不到任何“功能”允许您从沙箱内部执行此操作——在程序代码之外,您应该能够在IOT设备上打开PowerShell并运行
设置日期
命令。嗯,是的,PowerShell工作正常,我就是这样做的,但,如果没有有效的系统用户界面,就意味着无法作为一个独立的设备在其上获得准确的时间。希望他们能在发布前解决这个问题。时钟似乎会明显漂移,尤其是当设备断电后再次通电时。(如夜间断电导致近10分钟的错误)Pi没有RTC,因此每次启动时都会从NTP服务器获取时间。如果您对此不满意,您可以为Pi获取RTC附加组件,如此处所示:@dannykay1710从实证测试中,它似乎不会或通常不会自动从NTP获取数据。。。最后六个左右的版本一直都是在一个月前的同一时间启动并一直保持不变。问题是如何从应用程序中设置它。如评论中所述,我了解如何从powershell进行设置。但是,每次打开设备时,连接到powershell以设置时间是不切实际的。该设备只有在始终与计算机相连的情况下才可用,日期应介于引号之间:
set date“10/3/2015 2:00PM”
I使用->set date“8/25/2017 2:32PM”设置日期时间,但一段时间后会重置/更改。因此,需要tzutil来正确设置时区,因为Pi将根据时间服务器自动尝试更新当前日期时间。此“最佳答案”已过时。仅使用windows现在提供的DateTimeSettings.SetSystemDateNo API。您不能在UWP应用程序中调用PowerShell命令。根据:它通过ProcessLauncher()工作。只需调用script.cmd,然后使用PowerShell。有点复杂,但很有效。我将查看您的链接,谢谢。有趣的是,设置日期在win IoT中根本不起作用。当连接到IoT Hub的连接因SAS令牌过期而失败时,我将此答案用于强制时间同步
$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
 net start WinRM 
 Set-Item WSMan:\localhost\Client\TrustedHosts -Value <machine-name or IP Address>
Enter-PSSession -ComputerName <IP Address> -Credential localhost\Administrator
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;

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