C# IPAddress from string

Patrick Lorio picture Patrick Lorio · Sep 13, 2011 · Viewed 124k times · Source

I need to define the IP address in the class System.Net.IPAddress but the method:

IPAddress ipaddress = IPAddress.Parse("127.0.0.1");  //127.0.0.1 as an example

doesn't work, is there another method?

How can I define the IP address?

Answer

Jon Cage picture Jon Cage · Sep 13, 2011

You've probably miss-typed something above that bit of code or created your own class called IPAddress. If you're using the .net one, that function should be available.

Have you tried using System.Net.IPAddress just in case?

System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("127.0.0.1");  //127.0.0.1 as an example

The docs on Microsoft's site have a complete example which works fine on my machine.