NetworkInterface 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供網路介面的組態和統計資訊。
public ref class NetworkInterface abstract sealed
public ref class NetworkInterface abstract
public static class NetworkInterface
public abstract class NetworkInterface
type NetworkInterface = class
Public Class NetworkInterface
Public MustInherit Class NetworkInterface
- 繼承
-
NetworkInterface
範例
下列程式代碼範例會顯示介面的相關信息。
void ShowNetworkInterfaces()
{
IPGlobalProperties ^ computerProperties = IPGlobalProperties::GetIPGlobalProperties();
array<NetworkInterface^>^nics = NetworkInterface::GetAllNetworkInterfaces();
Console::WriteLine( "Interface information for {0}.{1} ", computerProperties->HostName, computerProperties->DomainName );
if ( nics == nullptr || nics->Length < 1 )
{
Console::WriteLine( " No network interfaces found." );
return;
}
Console::WriteLine( " Number of interfaces .................... : {0}", nics->Length );
System::Collections::IEnumerator^ myEnum4 = nics->GetEnumerator();
while ( myEnum4->MoveNext() )
{
NetworkInterface ^ adapter = safe_cast<NetworkInterface ^>(myEnum4->Current);
IPInterfaceProperties ^ properties = adapter->GetIPProperties();
Console::WriteLine();
Console::WriteLine( adapter->Description );
Console::WriteLine( String::Empty->PadLeft( adapter->Description->Length, '=' ) );
Console::WriteLine( " Interface type .......................... : {0}",
adapter->NetworkInterfaceType );
Console::WriteLine( " Physical Address ........................ : {0}",
adapter->GetPhysicalAddress() );
Console::WriteLine( " Operational status ...................... : {0}",
adapter->OperationalStatus );
String^ versions = "";
// Create a display string for the supported IP versions.
if ( adapter->Supports( NetworkInterfaceComponent::IPv4 ) )
{
versions = "IPv4";
}
if ( adapter->Supports( NetworkInterfaceComponent::IPv6 ) )
{
if ( versions->Length > 0 )
{
versions = String::Concat( versions, " " );
}
versions = String::Concat( versions, "IPv6" );
}
Console::WriteLine( " IP version .............................. : {0}",
versions );
ShowIPAddresses( properties );
// The following information is not useful for loopback adapters.
if ( adapter->NetworkInterfaceType == NetworkInterfaceType::Loopback )
{
continue;
}
Console::WriteLine( " DNS suffix .............................. : {0}",
properties->DnsSuffix );
String^ label;
if ( adapter->Supports( NetworkInterfaceComponent::IPv4 ) )
{
IPv4InterfaceProperties ^ ipv4 = properties->GetIPv4Properties();
Console::WriteLine( " MTU...................................... : {0}",
ipv4->Mtu );
if ( ipv4->UsesWins )
{
IPAddressCollection ^ winsServers = properties->WinsServersAddresses;
if ( winsServers->Count > 0 )
{
label = " WINS Servers ............................ :";
ShowIPAddresses( label, winsServers );
}
}
}
Console::WriteLine( " DNS enabled ............................. : {0}",
properties->IsDnsEnabled );
Console::WriteLine( " Dynamically configured DNS .............. : {0}",
properties->IsDynamicDnsEnabled );
Console::WriteLine( " Receive Only ............................ : {0}",
adapter->IsReceiveOnly );
Console::WriteLine( " Multicast ............................... : {0}",
adapter->SupportsMulticast );
ShowInterfaceStatistics( adapter );
Console::WriteLine();
}
}
public static void ShowNetworkInterfaces()
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
if (nics == null || nics.Length < 1)
{
Console.WriteLine(" No network interfaces found.");
return;
}
Console.WriteLine(" Number of interfaces .................... : {0}", nics.Length);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine();
Console.WriteLine(adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" Physical Address ........................ : {0}",
adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" Operational status ...................... : {0}",
adapter.OperationalStatus);
string versions ="";
// Create a display string for the supported IP versions.
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
}
if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions += " ";
}
versions += "IPv6";
}
Console.WriteLine(" IP version .............................. : {0}", versions);
ShowIPAddresses(properties);
// The following information is not useful for loopback adapters.
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
{
continue;
}
Console.WriteLine(" DNS suffix .............................. : {0}",
properties.DnsSuffix);
string label;
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
Console.WriteLine(" MTU...................................... : {0}", ipv4.Mtu);
if (ipv4.UsesWins)
{
IPAddressCollection winsServers = properties.WinsServersAddresses;
if (winsServers.Count > 0)
{
label = " WINS Servers ............................ :";
ShowIPAddresses(label, winsServers);
}
}
}
Console.WriteLine(" DNS enabled ............................. : {0}",
properties.IsDnsEnabled);
Console.WriteLine(" Dynamically configured DNS .............. : {0}",
properties.IsDynamicDnsEnabled);
Console.WriteLine(" Receive Only ............................ : {0}",
adapter.IsReceiveOnly);
Console.WriteLine(" Multicast ............................... : {0}",
adapter.SupportsMulticast);
ShowInterfaceStatistics(adapter);
Console.WriteLine();
}
}
備註
這個類別會在本機計算機上封裝網路介面的數據,也稱為適配卡。 您不會建立此類別的實例;方法 GetAllNetworkInterfaces 會傳回數位列,其中包含本機計算機上每個網路介面的這個類別實例。
建構函式
NetworkInterface() |
初始化 NetworkInterface 類別的新執行個體。 |
屬性
Description |
取得介面的描述。 |
Id |
取得網路介面卡的識別項。 |
IPv6LoopbackInterfaceIndex |
取得 IPv6 回送介面的索引。 |
IsReceiveOnly |
取得 Boolean 值,指出網路介面是否設定為只接收資料封包。 |
LoopbackInterfaceIndex |
取得 IPv4 回送介面的索引。 |
Name |
取得網路介面卡的名稱。 |
NetworkInterfaceType |
取得介面類型。 |
OperationalStatus |
取得網路連接的目前操作狀態。 |
Speed |
取得網路介面的速度。 |
SupportsMulticast |
取得 Boolean 值,指出網路介面是否啟用為可接收多點傳送封包。 |
方法
GetAllNetworkInterfaces() |
傳回物件,其描述本機電腦上的網路介面。 |
GetIPProperties() |
傳回物件,其描述這個網路介面的組態。 |
GetIPStatistics() |
取得這個 NetworkInterface 執行個體的 IP 統計資料。 |
GetIPv4Statistics() |
取得這個 NetworkInterface 執行個體的 IPv4 統計資料。 |
GetIsNetworkAvailable() |
指出是否有任何網路連接可使用。 |
GetPhysicalAddress() |
傳回這個介面卡的媒體存取控制 (MAC) 或實體位址。 |
Supports(NetworkInterfaceComponent) |
取得 Boolean 值,指出介面是否支援指定的通訊協定。 |