title: How to get current used network adapter
body: <p>Now I am trying to get network adapter in use among of several network adapters on my machine so that I can get ip address, mac address and adapter name of the network adapter. But I am not having any luck.
  What I found the solution for it is <a href="https://stackoverflow.com/questions/46548435/get-current-network-adapter-in-use#answer-46637015">Get current network adapter in use</a>.
  I dont think it is for .Net framework project but ASP.NET Core.</p>
  
  <p>My code is below.</p>
  
  <pre><code>public void GetIpAndMacAddress()
  {
      foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
      {
          // Only consider Ethernet network interfaces
          if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &amp;&amp;
              nic.OperationalStatus == OperationalStatus.Up)
          {
              IPInterfaceProperties adapterProperties = nic.GetIPProperties();
              foreach (var ip in adapterProperties.UnicastAddresses) 
              {
                  if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                      IPAddress = ip.Address.ToString();
              }
              string hexMac = nic.GetPhysicalAddress().ToString(),
                     regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})",
                     replace = "$1:$2:$3:$4:$5:$6";
              IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
  
              netAdapterName = nic.Name;
              MACAddress = Regex.Replace(hexMac, regex, replace);
              return;
          }
      }
  }
  </code></pre>