卡西卡的小寶庫
寶庫寶庫寶庫
Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

取得 IP, Hostname

Posted In: , , , . By 卡西卡

根據IP地址獲得主機名稱
// .NET 1.1 ~
System.Net.IPHostEntry host = System.Net.Dns.GetHostByAddress("192.168.0.1"); // [msdn]
Console.WriteLine(host.HostName);

// .NET 2.0 ~
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry("192.168.0.1"); // [msdn]
Console.WriteLine(host.HostName);

取得本機IP
static void Main(string[] args)
{
  // 顯示主機名
  string hostname = Dns.GetHostName();
  Console.WriteLine("hostname = {0}", hostname);

  // 顯示每個IP地址
  IPHostEntry hostent = Dns.GetHostByName(hostname); // 主機信息
  IPAddress[] addrs = hostent.AddressList; // IP地址數組
  foreach ( IPAddress ip in hostent.AddressList )
  {
    Console.WriteLine("Address: {0}", ip);
  } // foreach
}
 

VB, VBA

 

Ping 1.1

Posted In: , . By 卡西卡

使用範例:
public class MainProc
{
 public static void Main(string[] argv)
 {
  // 測試
  bool ret = NetworkHelper.Ping(argv[0]);
  Console.WriteLine("Ping [{0}] = {1}", argv[0], ret);
 }
}
 

注意事項:

  • .NET 2.0 以上有Ping Class可以直接使用。[msdn]

參考資料: