2017-03-28 38 views
0

到目前爲止,我一直在努力得到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消息。

回答

0

我認爲我必須做的是確保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; 
      } 
     } 
    }