【问题标题】:How can I determine bandwidth on Windows 7, 8.1 and 10?如何确定 Windows 7、8.1 和 10 上的带宽?
【发布时间】:2017-08-22 06:12:48
【问题描述】:

到目前为止,我一直在努力让 MbnInterfaceManager 正常工作(请参阅 hresult from IMbnInterfaceManager::GetInterfaces when no MBN device exists),因此我在 Visual Studio 2015 中构建并调试了一个没有问题的应用程序,该应用程序在 C# 中执行了此 WMI 查询(另请参阅 @987654322 @):

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

有人对如何解决此问题有任何建议吗?如何部署应用程序以便它能够使用这样的查询?

更新:

请注意,我可以在 Windows 7 或 Windows 8.1 上的 Visual Studio 2015 中构建和运行上述代码(作为更大的 WPF 应用程序的一部分),并且我可以使用 ClickOnce 将相同的应用程序部署到它所在的 Windows 7 上成功运行。出于某种原因,当我使用 ClickOnce 将此应用程序部署到 Windows 8.1 上时,我收到了 Invalid query 消息。

【问题讨论】:

    标签: c# visual-studio windows-8.1 wmi clickonce


    【解决方案1】:

    我认为我必须确保 System.Management 引用设置为“复制本地”,但我现在无法测试。如果有人有更好的想法,请随时告诉我。

    更新:

    在 Windows 8.1 上使用 System.Management.dll 的方式与在 Windows 7 或 Windows 10 上使用的方式不同。

    我发现要在 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";
                        }
                    // ...
                } 
    

    请注意,您必须确保您的项目是 Window 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;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2018-09-02
      • 2015-10-09
      • 2019-07-12
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多