Get host Name
var name = Dns.gethostname ();
Console.WriteLine ("User:" + name);
From the current host name to resolve the IP address, filter IPV4 address is the Local intranet IP address.
var IPv4 = dns.gethostentry (name). Addresslist.where (i = i.addressfamily = = addressfamily.internetwork). FirstOrDefault ();
Console.WriteLine ("Intranet IP:" + IPv4);
Console.WriteLine ("Set UPnP");
UPnP binding information
var eport = 8733;
var iport = 8733;
var description = "MGen test";
To create a COM type
var upnpnat = new Upnpnat ();
var mappings = Upnpnat. Staticportmappingcollection;
Error judgment
if (mappings = = null)
{
Console.WriteLine ("The router is not detected, or the router does not support UPnP functionality.") ");
Return
}
Add previous IPv4 variables (intranet IP), internal ports, and external ports
Mappings. ADD (Eport, "TCP", Iport, IPv4. ToString (), True, description);
Console.WriteLine ("External port: {0}", eport);
Console.WriteLine ("Internal port: {0}", Iport);
External IP variables
String Eip;
Regular
var regex = @ "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
using (var webclient = new WebClient ())
{
var rawres = webclient. Downloadstring ("http://checkip.dyndns.org/");
EIP = Regex.match (Rawres, Regex). Value;
}
Console.WriteLine ("Extranet IP:" + EIP);
C # UPnP