如何确定Windows 7,8.1和10上的带宽?
到目前为止,我一直在努力得到MbnInterfaceManager
工作(见hresult from IMbnInterfaceManager::GetInterfaces when no MBN device exists),所以不是我建立和调试从Visual Studio 2015年中,在C#中执行此WMI查询,没有任何问题的应用程序(另见Win32_PerfFormattedData_Tcpip_NetworkInterface
documentation):如何确定Windows 7,8.1和10上的带宽?
string query = "SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
但是当我将应用程序部署到Windows 8.1,我收到此错误每次执行查询时:
System.Management.ManagementException: Invalid query
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
有没有人对如何解决这个问题有什么建议?我怎样才能部署一个应用程序,以便它能够使用这样的查询?
UPDATE:
请注意,我可以从Visual Studio 2015年中在Windows 7或Windows 8.1完成构建并运行上面的代码(如较大的WPF应用程序的一部分),我可以部署同样的应用程序使用ClickOnce到Windows 7上运行成功。出于某种原因,当我在Windows 8.1上使用ClickOnce部署此应用程序时,我收到了Invalid query
消息。
我认为我必须做的是确保System.Management
引用设置为“复制本地”,但我现在无法对其进行测试。如果有人有任何更好的想法,请随时让我知道。
UPDATE:
不可能在它是在Windows 7或Windows 10
使用的相同方式使用System.Management.dll在Windows 8.1中,我发现,执行与我在Windows 8.1和Windows 8手机上提到的问题类似的操作,您需要获得Windows 8.1开发人员许可证,或者在Windows 10上将计算机设置为“开发人员模式”,以便可以使用Windows.Networking.Connectivity
命名空间:
string connectionProfileInfo = string.Empty;
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
}
else
{
connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
OutputText.Text = connectionProfileInfo;
rootPage.NotifyUser("Success", NotifyType.StatusMessage);
}
// Which calls this function, that allows you to determine how strong the signal is and the associated bandwidth
string GetConnectionProfile(ConnectionProfile connectionProfile)
{
// ...
if (connectionProfile.GetSignalBars().HasValue)
{
connectionProfileInfo += "====================\n";
connectionProfileInfo += "Signal Bars: " + connectionProfile.GetSignalBars() + "\n";
}
// ...
}
请注意,您必须确保您的项目是Windows 8.1 PCL或Windows 8.1应用程序才能够引用命名空间。
有关详细信息,请参阅https://code.msdn.microsoft.com/windowsapps/network-information-sample-63aaa201
更新2:
为了能够获得在Windows 7上,8.1和10个带宽,我结束了使用此代码:
private int GetMaxBandwidth()
{
int maxBandwidth = 0;
NetworkInterface[] networkIntrInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in networkIntrInterfaces)
{
IPv4InterfaceStatistics interfaceStats = networkInterface.GetIPv4Statistics();
int bytesSentSpeed = (int)(interfaceStats.BytesSent);
int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived);
if (bytesSentSpeed + bytesReceivedSpeed > maxBandwidth)
{
maxBandwidth = bytesSentSpeed + bytesReceivedSpeed;
}
}
}