How can I determine bandwidth on Windows 7, 8.1 and 10?

巧了我就是萌 提交于 2019-12-25 16:17:12

问题


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();

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?

UPDATE:

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.


回答1:


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.

UPDATE:

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.

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";
                    }
                // ...
            } 

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.

For details please see https://code.msdn.microsoft.com/windowsapps/network-information-sample-63aaa201

UPDATE 2:

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;
            }
        }
    }


来源:https://stackoverflow.com/questions/43081953/how-can-i-determine-bandwidth-on-windows-7-8-1-and-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!