NetworkInterface etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
NetworkInterface etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

8 Şubat 2017 Çarşamba

NetworkInterface Sınıfı

Giriş
Şu satırı dahil ederiz.
using System.Net.NetworkInformation.NetworkInterface;
Java'daki NetworkInterface Sınıfı ile aynı işlevi görür.

GetAllNetworkInterfaces metodu
Şöyle yaparız.
NetworkInterface netif =NetworkInterface.GetAllNetworkInterfaces()
GetIPProperties metodu
IP özelliklerini döner. Şöyle yaparız.
IPInterfaceProperties properties = nic.GetIPProperties();
Eğer arayüzünü ismini biliyorsak şöyle yaparız.
NetworkInterface netif = NetworkInterface.GetAllNetworkInterfaces()
 .Single(networkInterface => networkInterface.Name.ToLower() =="loopback");

IPInterfaceProperties propss = netif.GetIPProperties();
UnicastIPAddressInformationCollection unicastIpAddress = props.UnicastAddresses;

Console.Write(netif.Name + ": ");
Console.WriteLine(unicastIpAddress[1].Address);
İstediğimiz arayüzlerin IP adreslerini şöyle alırız.Önce bir arayüz isimlerini bir diziye doldururuz.
var networkInterfaceNames = new HashSet<string>() {
  "Local Area Connection",
  "Loopback Pseudo-Interface 1"
};
Daha sonra bu isimlere sahip arayüzleri seçeriz.
var localIpAddresses = NetworkInterface
  .GetAllNetworkInterfaces()
  .Where(ni => networkInterfaceNames.Contains(ni.Name))
  .Select(
    ni => ni
      .GetIPProperties()
      .UnicastAddresses
      .First(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork)
  );
GetIsNetworkAvailable metodu - Tüm Arayüzler İçin
Şöyle yaparız.
if (NetworkInterface.GetIsNetworkAvailable()) {...}
GetIsNetworkAvailable metodu - Tek Bir Arayüz İçin
Arayüzün "up" veya "down" olduğunu bildirir.
Console.WriteLine("Network interface {0} is {1}", 
                           adapter.Id,
                           (adapter.GetIsNetworkAvailable()?"up":"down")
                         );
GetPyhsicalAddress metodu
Şöyle yaparız.
macAddr =
    (
        from nic in NetworkInterface.GetAllNetworkInterfaces()
        where nic.OperationalStatus == OperationalStatus.Up
        select nic.GetPhysicalAddress().ToString()

    ).FirstOrDefault();
Eğer Linq kullanmak istemezsek şöyle yaparız.
foreach(var nic in NetworkInterface.GetAllNetworkInterfaces())
{
   if(nic.OperationalStatus == OperationalStatus.Up)
   {
    return nic.GetPhysicalAddress();
   }
}
return string.Empty();
Id Alanı
Her arayüzün bir numarası vardır.

Name Alanı
Her arayüzün bir ismi vardır. Şöyle yaparız.
NetworkInterface netif =NetworkInterface.GetAllNetworkInterfaces()
 .Single(networkInterface => networkInterface.Name.ToLower() =="loopback");
Type Alanı
Şöyle yaparız.
if (netif.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 
     || netif.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
  ...
}
Şöyle yaparız.
NetworkInterface ni = ...;
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 &&
    ni.OperationalStatus    == OperationalStatus.Up) {
  ...
}


21 Mart 2016 Pazartesi

Ağ Arayüzleri

NetworkInterface sınıfı
NetworkInterface Sınıfı yazısına taşıdım.

MAC Adresini Alma
WMI ile ağ arayüzleri dolaşılır
ManagementObjectSearcher mos = new ManagementObjectSearcher
("select * from Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mo in mos.Get())
{
  string mac = mo["MACAddress"].ToString();
  
}
IP Adresi Atama
Bazen statik bir IP adresi atamak isteriz.WMI kullanılabilir.
private static void SetIP(string ip_address, string subnet_mask)
{
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection objMOC = mc.GetInstances();

  foreach (ManagementObject objMO in objMOC) {
    if ((bool)objMO("IPEnabled")) {
      try {
        ManagementBaseObject setIP = default(ManagementBaseObject);
        ManagementBaseObject newIP = objMO.GetMethodParameters("EnableStatic");

        newIP("IPAddress") = new string[] { ip_address };
        newIP("SubnetMask") = new string[] { subnet_mask };

        setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
      }
      catch (Exception generatedExceptionName) {
        throw;
      }
    }

  }
}
Arayüzün Bağlı Olduğunu AnlamaÖrnek
ConnectionProfile internetCon = GetInternetConnectionProfile();

if (internetConnectionProfile != null)
{
    this.IsInternetAvailable = internetConn.GetNetworkConnectivityLevel() ==
                                         NetworkConnectivityLevel.InternetAccess;

   if (internetConnec.NetworkAdapter.IanaInterfaceType != 71) 
   {
     var isRoaming = internetConnectionProfile.GetConnectionCost().Roaming;

    //user is Low on Data package only send low data.
    var isLowOnData = internetConn.GetConnectionCost().ApproachingDataLimit;

    //User is over limit do not send data
    var isOverDataLimit = internetConn.GetConnectionCost().OverDataLimit;
    IsWifiConnected = true;
  }
  else //Connection is a Wi-Fi connection. Data restrictions are not necessary. 
  {
    IsWifiConnected = true;

  }
}