[C#] C#实现的FastPing代码 →→→→→进入此内容的聊天室

来自 , 2020-10-17, 写在 C#, 查看 154 次.
URL http://www.code666.cn/view/e287f0b2
  1. using System;
  2. using System.Net.NetworkInformation;
  3.  
  4. namespace RobvanderWoude
  5. {
  6.         class FastPing
  7.         {
  8.                 static int Main( string[] args )
  9.                 {
  10.                         try
  11.                         {
  12.                                 string hostname = string.Empty;
  13.                                 char[] test = { '/', '?' };
  14.  
  15.                                 #region Command Line Parsing
  16.  
  17.                                 if ( args.Length == 1 )
  18.                                 {
  19.                                         hostname = args[0];
  20.                                 }
  21.                                 else
  22.                                 {
  23.                                         return WriteError( );
  24.                                 }
  25.                                 if ( hostname.IndexOfAny( test ) != -1 )
  26.                                 {
  27.                                         return WriteError( );
  28.                                 }
  29.                                 #endregion Command Line Parsing
  30.  
  31.                                 try
  32.                                 {
  33.                                         Ping ping = new Ping( );
  34.                                         PingReply reply = ping.Send( hostname );
  35.                                         Console.WriteLine( reply.Address );
  36.                                         if ( reply.Status == IPStatus.Success )
  37.                                         {
  38.                                                 return 0;
  39.                                         }
  40.                                         else
  41.                                         {
  42.                                                 return 1;
  43.                                         }
  44.                                 }
  45.                                 catch ( PingException e )
  46.                                 {
  47.                                         Console.Error.WriteLine( "ERROR: {0} ({1})", e.Message, e.InnerException.Message );
  48.                                         return 1;
  49.                                 }
  50.  
  51.                         }
  52.                         catch ( Exception e )
  53.                         {
  54.                                 Console.Error.WriteLine( "ERROR: {0}", e.Message );
  55.                                 return 1;
  56.                         }
  57.                 }
  58.  
  59.        
  60.                 #region Error Handling
  61.  
  62.                 public static int WriteError( Exception e = null )
  63.                 {
  64.                         return WriteError( e == null ? null : e.Message );
  65.                 }
  66.  
  67.                 public static int WriteError( string errorMessage )
  68.                 {
  69.                         Console.OpenStandardError( );
  70.                         if ( string.IsNullOrEmpty( errorMessage ) == false )
  71.                         {
  72.                                 Console.Error.WriteLine( );
  73.                                 Console.ForegroundColor = ConsoleColor.Red;
  74.                                 Console.Error.Write( "ERROR: " );
  75.                                 Console.ForegroundColor = ConsoleColor.White;
  76.                                 Console.Error.WriteLine( errorMessage );
  77.                                 Console.ResetColor( );
  78.                         }
  79.  
  80.                         /*
  81.                         FastPing,  Version 1.00
  82.                         Faster PING alternative
  83.  
  84.                         Usage:   FASTPING    hostname
  85.                         or:      FASTPING    ipaddress
  86.  
  87.                         Where:   hostname    is the host name to be pinged
  88.                                  ipaddress   is the IP address to be pinged
  89.  
  90.                         Written by Rob van der Woude
  91.                         http://www.robvanderwoude.com
  92.                         */
  93.  
  94.                         Console.Error.WriteLine( );
  95.                         Console.Error.WriteLine( "FastPing,  Version 1.00" );
  96.                         Console.Error.WriteLine( "Faster PING alternative" );
  97.                         Console.Error.WriteLine( );
  98.                         Console.Error.Write( "Usage:   " );
  99.                         Console.ForegroundColor = ConsoleColor.White;
  100.                         Console.Error.WriteLine( "FASTPING  hostname" );
  101.                         Console.ResetColor( );
  102.                         Console.Error.Write( "or:      " );
  103.                         Console.ForegroundColor = ConsoleColor.White;
  104.                         Console.Error.WriteLine( "FASTPING  ipaddress" );
  105.                         Console.ResetColor( );
  106.                         Console.Error.WriteLine( );
  107.                         Console.Error.Write( "Where:   " );
  108.                         Console.ForegroundColor = ConsoleColor.White;
  109.                         Console.Error.Write( "hostname" );
  110.                         Console.ResetColor( );
  111.                         Console.Error.WriteLine( "      is the host name to be pinged" );
  112.                         Console.ForegroundColor = ConsoleColor.White;
  113.                         Console.Error.Write( "         ipaddress" );
  114.                         Console.ResetColor( );
  115.                         Console.Error.WriteLine( "     is the IP address to be pinged" );
  116.                         Console.Error.WriteLine( );
  117.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  118.                         Console.Error.Write( "http://www.robvanderwoude.com" );
  119.                         Console.OpenStandardOutput( );
  120.                         return 1;
  121.                 }
  122.  
  123.                 #endregion Error Handling
  124.         }
  125. }
  126. //csharp/7332

回复 "C#实现的FastPing代码"

这儿你可以回复上面这条便签

captcha