Getting IPGlobalProperties with VB.net on a UWP ap

2019-03-06 18:55发布

I am writing a very simple Universal Windows application with VB in Visual Studio 2017. The application should gave basic network information to the user, so I wanted to collect the data using IPGlobalProperties and print – as a first example – the DomainName in a TextBlock called textDomain

Dim NetworkProperties As NetworkInformation.IPGlobalProperties
NetworkProperties = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties
textDomain.Text = NetworkProperties.DomainName

While the properties are correctly assigned int the first two lines of code, the third line result in the error System.PlatformNotSupportedException: 'Operation is not supported on this platform.' I tried the same code on a classic Windows application and it works as intended, so is this operation not supported by Universal applications? If yes, what is the method I should use to get the same information about network?

Thanks for any help you might provide

Luca

1条回答
家丑人穷心不美
2楼-- · 2019-03-06 19:43

Firstly, IPGlobalProperties class is not supported in uwp app. Since.NET for UWP apps provides a set of managed types that you can use to create Universal Windows Platform apps, but not the all. Details please reference .NET for UWP apps. System.Net namespace can be used in uwp app but IPGlobalProperties cannot.

Secondly you can find equivalent or similar APIs in uwp app. For example you can also find a NetworkInformation in uwp which is belong to Windows.Networking.Connectivity namespace. But invoking the methods of this class is not the same way with the System.Net.NetworkInformation namespace.

If you want to get the computer name or domain name as IPGlobalProperties did, you may invoke the GetHostNames() method. Code as follows:

Imports Windows.Networking.Connectivity
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Page

    Private Sub btngetinfo_Click(sender As Object, e As RoutedEventArgs)  
        Dim hostNames = NetworkInformation.GetHostNames()
        textDomain.Text = hostNames.First.ToString
    End Sub
End Class
查看更多
登录 后发表回答