如何确定Windows 7、8.1和10的带宽? [英] How can I determine bandwidth on Windows 7, 8.1 and 10?
问题描述
到目前为止,我一直在努力使 MbnInterfaceManager
工作(请参阅),所以我在Visual Studio 2015中构建并调试了没有问题的应用程序,该应用程序在C#(另请参见 Win32_PerfFormattedData_Tcpip_NetworkInterface
文档):
So far I have struggled to get MbnInterfaceManager
working (see hresult from IMbnInterfaceManager::GetInterfaces when no MBN device exists), so instead I built and debugged an application with no problems from within Visual Studio 2015 that executed this WMI query in C# (see also the Win32_PerfFormattedData_Tcpip_NetworkInterface
documentation):
string query = "SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
但是当我将应用程序部署到Windows 8.1时,每次执行查询时都会收到此错误:
But then when I deployed the application to Windows 8.1, I receive this error every time the query is executed:
System.Management.ManagementException: Invalid query
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
有人对吗?如何解决这个问题?如何部署应用程序,使其能够使用这样的查询?
Does anyone have any suggestions on how to resolve this issue? How can I deploy an application so that it is able to use queries like this?
更新:
请注意,我可以在Windows 7或Windows 8.1上的Visual Studio 2015中构建并运行以上代码(作为大型WPF应用程序的一部分),并且可以使用ClickOnce将相同的应用程序部署到Windows上7它成功运行的地方。出于某种原因,当我使用ClickOnce将此应用程序部署到Windows 8.1时,收到了无效查询
消息。
Please note that I can build and run the above code (as part of a larger WPF application) from within Visual Studio 2015 on either Windows 7 or Windows 8.1, and I can deploy the same application using ClickOnce onto Windows 7 where it runs successfully. For some reason when I deploy this application using ClickOnce onto Windows 8.1, I get that Invalid query
message.
推荐答案
我认为我需要做的是确保将 System.Management
引用设置为 Copy Local,但我没有能够立即进行测试。如果有人有更好的主意,请随时告诉我。
I think what I have to do is make sure that the System.Management
reference is set to "Copy Local" but I'm not able to test that right now. If anyone has any better ideas please feel free to let me know.
更新:
在Windows 8.1上无法以与在Windows 7或Windows 10中相同的方式使用System.Management.dll。
It is not possible to use System.Management.dll on Windows 8.1 in the same way it is used on Windows 7 or Windows 10.
我发现要执行与我在Windows 8.1和Windows 8手机上的问题中提到的操作类似的操作,您需要获取Windows 8.1开发人员许可,或者在Windows 10上将计算机设置为开发人员模式,以便可以使用 Windows.Networking.Connectivity
命名空间:
I've found that to perform operations similar to the ones I mentioned in my question on Windows 8.1 and Windows 8 phone you need to either get a Windows 8.1 developer license or on Windows 10 set your computer to "Developer Mode" so you can use the Windows.Networking.Connectivity
namespace:
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应用程序,才能引用命名空间。
Please note that you have to make sure your project is either a Window 8.1 PCL or a Windows 8.1 app to be able to reference the namespace.
有关详细信息,请参见 https://code.msdn.microsoft.com/windowsapps/network-information-sample- 63aaa201
For details please see https://code.msdn.microsoft.com/windowsapps/network-information-sample-63aaa201
更新2:
在Windows 7、8.1和10的带宽上,我最终使用以下代码:
To be able to get bandwidth on Windows 7, 8.1 and 10, I ended up using this code:
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;
}
}
}
这篇关于如何确定Windows 7、8.1和10的带宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!