[.NET] 取得網卡連線狀態

摘要:[.NET] : 取得網卡連線狀態


using System.Net.NetworkInformation;

public static class NetworkInterfaceUtility
{
    // GetOperationalStatus
    public static OperationalStatus GetOperationalStatus(string macAddress)
    {
        #region Require

        if (string.IsNullOrEmpty(macAddress) == true) throw new ArgumentNullException();

        #endregion
        return GetOperationalStatus(PhysicalAddress.Parse(macAddress));
    }

    public static OperationalStatus GetOperationalStatus(PhysicalAddress macAddress)
    {
        #region Require

        if (macAddress == null) throw new ArgumentNullException();

        #endregion
        foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (PhysicalAddress.Equals(macAddress, networkInterface.GetPhysicalAddress()) == true)
            {
                return networkInterface.OperationalStatus;
            }
        }
        return OperationalStatus.Unknown;
    }
}
期許自己
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。