Пример #1
0
        public static void CreateRoute(string destination, string mask, int interfaceIndex, int metric)
        {
            NetworkAdaptor adaptor = NicInterface.GetNetworkAdaptor(interfaceIndex);
            var            route   = new NativeMethods.MIB_IPFORWARDROW
            {
                dwForwardDest    = BitConverter.ToUInt32(IPAddress.Parse(destination).GetAddressBytes(), 0),
                dwForwardMask    = BitConverter.ToUInt32(IPAddress.Parse(mask).GetAddressBytes(), 0),
                dwForwardNextHop = BitConverter.ToUInt32(IPAddress.Parse(adaptor.PrimaryGateway.ToString()).GetAddressBytes(), 0),
                dwForwardMetric1 = Convert.ToUInt32(metric),
                dwForwardType    = Convert.ToUInt32(3), //Default to 3
                dwForwardProto   = Convert.ToUInt32(3), //Default to 3
                dwForwardAge     = 0,
                dwForwardIfIndex = Convert.ToUInt32(interfaceIndex)
            };

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MIB_IPFORWARDROW)));

            try
            {
                Marshal.StructureToPtr(route, ptr, false);
                var status = NativeMethods.CreateIpForwardEntry(ptr);
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
Пример #2
0
        public static void PrintAllNetworkInterface()
        {
            List <NetworkAdaptor> naList = NicInterface.GetAllNetworkAdaptor();

            Console.WriteLine("{0,18} {1,18} {2,18} {3,20} {4,6} {5}", "IP Address", "Subnet Mask", "Gateway", "MAC", "IF", "Name");
            foreach (NetworkAdaptor na in naList)
            {
                Console.WriteLine("{0,18} {1,18} {2,18} {3,20} {4,6} {5}", na.PrimaryIpAddress, na.SubnetMask, na.PrimaryGateway, na.MACAddress, na.InterfaceIndex, na.Name);
            }
        }