當前位置: 首頁>>代碼示例>>C#>>正文


C# IPAddress.Parse方法代碼示例

本文整理匯總了C#中System.Net.IPAddress.Parse方法的典型用法代碼示例。如果您正苦於以下問題:C# IPAddress.Parse方法的具體用法?C# IPAddress.Parse怎麽用?C# IPAddress.Parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.IPAddress的用法示例。


在下文中一共展示了IPAddress.Parse方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Net;

class ParseAddress
{

  private static void Main(string[] args) 
  {
    string IPaddress;

    if (args.Length == 0)
    {
      Console.WriteLine("Please enter an IP address.");
      Console.WriteLine("Usage:   >cs_parse any IPv4 or IPv6 address.");
      Console.WriteLine("Example: >cs_parse 127.0.0.1");
      Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1");
      return;
    }
    else
        {
            IPaddress = args[0];
        }

        // Get the list of the IPv6 addresses associated with the requested host.
        Parse(IPaddress);
  }

  // This method calls the IPAddress.Parse method to check the ipAddress 
  // input string. If the ipAddress argument represents a syntatically correct IPv4 or
  // IPv6 address, the method displays the Parse output into quad-notation or
  // colon-hexadecimal notation, respectively. Otherwise, it displays an 
  // error message.
  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);
    }
   }
}
開發者ID:.NET開發者,項目名稱:System.Net,代碼行數:67,代碼來源:IPAddress.Parse

示例2: IPAddress.Parse(String ipAddress)

//引入命名空間
using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");

        Console.WriteLine(test1);
    }
}
開發者ID:C#程序員,項目名稱:System.Net,代碼行數:13,代碼來源:IPAddress.Parse


注:本文中的System.Net.IPAddress.Parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。