在 UWP 应用程序上使用 VB.net 获取 IPGlobalProperties [英] Getting IPGlobalProperties with VB.net on a UWP application
问题描述
我正在 Visual Studio 2017 中使用 VB 编写一个非常简单的通用 Windows 应用程序.应用程序应该向用户提供基本的网络信息,因此我想使用 IPGlobalProperties
收集数据并打印 - 作为第一个示例 - 名为 DomainName
>textDomain
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
虽然属性在前两行代码中正确分配,但第三行导致错误 System.PlatformNotSupportedException: 'Operation is not supported on this platform.'
我在经典的 Windows 应用程序上尝试了相同的代码,它按预期工作,那么通用应用程序不支持此操作吗?如果是,我应该使用什么方法来获取有关网络的相同信息?
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?
感谢您提供的任何帮助
卢卡
推荐答案
首先,IPGlobalProperties
类在 uwp 应用程序中不受支持.由于 .NET for UWP 应用程序提供了一组托管类型,可用于创建通用 Windows 平台应用程序,但不是全部.详细信息请参考 .NET 的 UWP 应用.System.Net
命名空间可以在 uwp 应用程序中使用,但 IPGlobalProperties
不能.
其次,您可以在 uwp 应用程序中找到等效或相似的 API.例如,您还可以找到 NetworkInformation
属于 Windows.Networking.Connectivity
命名空间.但是调用这个类的方法和System.Net.NetworkInformation
命名空间.
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.
如果您想像 IPGlobalProperties
那样获取计算机名称或域名,您可以调用 GetHostNames()
方法.代码如下:
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
这篇关于在 UWP 应用程序上使用 VB.net 获取 IPGlobalProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!