从Windows IoT核心版中关闭Raspbian

时间:2018-12-29 15:59:39

标签: uwp raspbian windows-10-iot-core

我正在Visual Studio上构建Windows UWP应用程序,该应用程序在Raspberry Pi 3B上运行。由此,我想关闭/重启另一个运行Raspbian的Raspberry Pi。 那有可能吗? 预先谢谢你

1 个答案:

答案 0 :(得分:3)

是的,有可能。您可以在UWP应用中使用SSH客户端(例如 Chilkat.Ssh Asmodat.Standard.SSH.NET )连接到Raspbian,然后发送shutdown / reboot命令(例如sudo shutdown –h now)。 Asmodat.Standard.SSH.NET(如果有免费库),但Chilkat.Ssh不是。您可以找到其他一些可以支持.Net Standard 2.0 / UAP 10的SSH库。

更新

以下代码段与Asmodat.Standard.SSH.NET库一起使用。它可以在运行在Windows IoT核心版上的UWP应用中使用。

C#

    private async void ButtonReboot_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,  async () =>
        {
            using (var client = new SshClient("xxx.xxx.xxx.xxx", "xx", "xxxxxxx"))
            {
                client.Connect();
                var command = client.CreateCommand("sudo reboot -f");
                var asyncResult = command.BeginExecute();
                var reader = new StreamReader(command.OutputStream);

                while (!asyncResult.IsCompleted)
                {
                    await reader.ReadToEndAsync();
                }

                command.EndExecute(asyncResult);

                client.Disconnect();
            }
        });
    }

VB.NET

Private Async Sub ButtonReboot_Click(sender As Object, e As RoutedEventArgs)

    Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
            Async Sub()
                Using sClient = New SshClient("XXX.XXX.XXX", "XX", "XXXX")
                    sClient.Connect()
                    If sClient.IsConnected Then
                        Dim sCommand = sClient.CreateCommand("sudo reboot -f \r\n")
                        Dim asyncResult = sCommand.BeginExecute()
                        Dim reader = New StreamReader(sCommand.OutputStream)

                        While Not asyncResult.IsCompleted
                            Await reader.ReadToEndAsync()
                        End While

                        sCommand.EndExecute(asyncResult)

                        Debug.WriteLine("Reboot")
                        sClient.Disconnect()
                    End If
                End Using
            End Sub)

End Sub
相关问题