Try this one. (credits to Zach)
Make sure to guard the sensible parts with try/catch
using System;
using System.Net;
using System.Runtime.InteropServices;
namespace CreateArpEntry
{
class Program
{
[DllImport("iphlpapi.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int CreateIpNetEntry(ref MIB_IPNETROW tableEntry);
static void Main(string[] args)
{
IPAddress.TryParse("192.168.1.2", out IPAddress parsedIpAddress);
byte[] macBytes = {0,0x0b,0x3b,0,0,1,0,0}; // pad the MAC address with zeros
MIB_IPNETROW arpEntry = new MIB_IPNETROW
{
Index = 20,
Addr = BitConverter.ToInt32(parsedIpAddress.GetAddressBytes(), 0),
PhysAddr = macBytes,
PhysAddrLen = 8,
Type = 4, // it can only be static
};
int a = CreateIpNetEntry(ref arpEntry);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPNETROW
{
/// <summary>
/// The index of the adapter (Not constant may change for example by disable / enable
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public int Index;
/// <summary>
/// The length of the physical address ( MAC Address)
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public int PhysAddrLen;
/// <summary>
/// The physical / MAC Address
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] PhysAddr;
/// <summary>
/// The IP Address
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public int Addr;
/// <summary>
/// The IP Address Type
/// Other = 1, Invalid = 2, Dynamic = 3, Static = 4
/// </summary>
[MarshalAs(UnmanagedType.U4)]
public int Type;
}
}