How to determine if a string is a valid IPv4 or IPv6 address in C#?

前端 未结 7 1292
逝去的感伤
逝去的感伤 2020-12-23 18:48

I know regex is dangerous for validating IP addresses because of the different forms an IP address can take.

I\'ve seen similar questions for C and C++, and those we

7条回答
  •  无人及你
    2020-12-23 19:32

    Just a warning about using System.Net.IpAddress.TryParse():

    If you pass it an string containing an integer (e.g. "3") the TryParse function will convert it to "0.0.0.3" and, therefore, a valid InterNetworkV4 address. So, at the very least, the reformatted "0.0.0.3" should be returned to the user application so the user knows how their input was interpreted.

提交回复
热议问题