NetworkInformation.GetInternetConnectionProfile 方法

定義

擷取與本機電腦目前使用的 用介面相關聯的連線設定檔。 慣用的介面是最有可能傳送或接收網際網路流量的介面。 這表示傳回的設定檔可能或可能沒有網際網路存取。

public:
 static ConnectionProfile ^ GetInternetConnectionProfile();
 static ConnectionProfile GetInternetConnectionProfile();
public static ConnectionProfile GetInternetConnectionProfile();
function getInternetConnectionProfile()
Public Shared Function GetInternetConnectionProfile () As ConnectionProfile

傳回

慣用 介面相關聯的連線設定檔,如果沒有具有適當連線的連接設定檔,則為 null

備註

若要收到連線變更的通知,請使用 NetworkStatusChanged 事件註冊處理常式。 收到網路狀態變更的通知時,請呼叫 GetInternetConnectionProfile 方法來取得目前的連線設定檔,然後儲存以供稍後在需要時使用。

下列範例示範如何擷取 ConnectionProfile。 函式會呼叫 GetInternetConnectionProfile ,以擷取代表目前最可能用於網際網路連線之連線的 ConnectionProfile 。 或者,您的應用程式可以呼叫 GetConnectionProfiles 來擷取裝置上所有可用的連線。

注意

如需下列程式碼中 GetConnectionProfileInfo 方法的實作,以及如何實作 NetworkInformation 類別方法以擷取連線設定檔的其他範例,請參閱 快速入門:擷取網路連線資訊

using Windows.Networking.Connectivity;

// Retrieve the ConnectionProfile.
ConnectionProfile internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

// Pass the returned object to a function that accesses the connection data.
string connectionProfileInfo = GetConnectionProfileInfo(internetConnectionProfile);
#include <winrt/Windows.Networking.Connectivity.h>
using namespace winrt;

std::wstring GetConnectionProfileInfo(Windows::Networking::Connectivity::ConnectionProfile const& connectionProfile)
{
    ...
}

int main()
{
    winrt::init_apartment();

    // Retrieve the ConnectionProfile.
    auto internetConnectionProfile{ Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile() };

    // Pass the returned object to a function that accesses the connection data.
    std::wstring strConnectionProfileInfo{ GetConnectionProfileInfo(internetConnectionProfile) };
}
using namespace Windows::Networking::Connectivity;

// Retrieve the ConnectionProfile.
ConnectionProfile^ internetConnectionProfile = NetworkInformation::GetInternetConnectionProfile();

// Pass the returned object to a function that accesses the connection data.
String^ strConnectionProfileInfo = GetConnectionProfileInfo(internetConnectionProfile);

如果目前有多個連線設定檔將電腦連線到網際網路,此方法會傳回慣用的設定檔。 慣用設定檔代表在傳送和接收網際網路流量時最常使用的連線。

適用於