c#:文字列からIPアドレスを取得、検査

イヤー有ったのか今知りました。
文字列からIPアドレスを取得や検査を行う。
今までは、正規表現で駆使してたけど、IPv6でちょっと止めていたが、
やろーかと思った勢いで調べた出来ました。
using System;
using System.Net;
//標準メソッド
ipAddressは混在でも良い変換できなかったらエラーなので、拾えばいい。
IPAddress address = IPAddress.Parse(ipAddress);

//以下がMSDN無くなったら困るのでメモ
parse(IPaddress);

private static void parse(string ipAddress)
{
try
{
// Create an instance of IPAddress for the specified address string (in
// dotted-quad, or colon-hexadecimal notation).
IPAddress address = IPAddress.Parse(ipAddress);

// Display the address in standard notation.
Console.WriteLine(“Parsing your input string: ” + “\”” + ipAddress + “\”” + ” produces this address (shown in its standard notation): “+ address.ToString());
}

catch(ArgumentNullException e)
{
Console.WriteLine(“ArgumentNullException caught!!!”);
Console.WriteLine(“Source : ” + e.Source);
Console.WriteLine(“Message : ” + e.Message);
}

catch(FormatException e)
{
Console.WriteLine(“FormatException caught!!!”);
Console.WriteLine(“Source : ” + e.Source);
Console.WriteLine(“Message : ” + e.Message);
}

catch(Exception e)
{
Console.WriteLine(“Exception caught!!!”);
Console.WriteLine(“Source : ” + e.Source);
Console.WriteLine(“Message : ” + e.Message);
}

}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です