獲得電腦名稱和IP地址

來源:互聯網
上載者:User
獲得電腦名稱和IP地址

源作者:追風                   人氣:9779

VisualC#是微軟公司推出的下一代程式開發語言,是微軟.Net 架構中的的一個重要組成部分,在推出VisualC#的過程中,微軟公司還推出了與之相對應的一個軟體開發包--.Net FrameWorkSDK。此軟體開發包裡面封裝了許多類、對象。Visual C#就是通過調用這些類、對象來實現許多比較強大的功能。
在.Net FrameWork SDK中提供了二個可用於網路編程的名稱空間,一個是System.Net,另一個是System..Net.Socket。本文就是利用第一個名稱空間中封裝的類和對象來讀取本機電腦名稱和機器中所有的IP地址。
一.概述:
我們知道對於一台電腦來說,他只有一個電腦名稱,但是他可以有多個IP地址。例如當電腦通過撥接的時候,在驗證完使用者名稱和口令以後,就會動態分配一個IP地址,此時電腦就擁有了二個IP地址,一個時自己設定的區域網路用的IP地址,另外一個就是撥接動態分配的IP地址了。本文就是來探索一下如何讀取此二個IP地址和電腦名稱。
二.程式設計和啟動並執行環境:
(1)微軟公司視窗2000伺服器版
(2).Net FrameWrok SDK Beta 2版
三.程式設計的主要思路及實現方法:
(1).讀取電腦的名稱:
在名稱空間System.Net中定義了一個類Dns,在此類中定義了一個比較重要的方法 GetHostName (),此方法的傳回值就是本機電腦名稱。在程式設計中首先要匯入System.Net名稱空間,然後通過調用Dns類中的GetHostName ()方法讀取本機電腦名稱,具體實現的主要語句如下:
label1.Text = "主機名稱:" + System.Net.Dns.GetHostName ( ) ;
(2).讀取電腦的撥接臨時的IP地址和區域網路分配的固定IP地址:
在程式設計中,我們是通過一個自訂的函數--getIPAddress ( )來讀取IP地址的。首先看一下如何讀取本地固定的IP地址的。在Dns類中還定義了一個方法GetHostByName( )。此方法的傳回值時IPHostEntry 對象,此對象有一個屬性是AddressList,此屬性是一個IPAddress類型的數組,包含了電腦此時的所有的IP地址資訊。這當中也就包含了撥接得到的臨時分配的IP地址和區域網路固定的IP地址。具體實現語句如下:
private static stringgetIPAddress ( )
{
System.Net.IPAddress addr;
// 獲得本機區域網路IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName (Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
return addr.ToString ( ) ;
}
來源程式如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;

namespace GetIP
{
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.ComponentModel.Container components = null;
  public Form1()
  {   
   InitializeComponent();
   label1.Text=System.Net.Dns.GetHostName();
   label2.Text=getIpAddress();
  }
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.SuspendLayout();

   this.label1.Location = new System.Drawing.Point(48, 24);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(264, 24);
   this.label1.TabIndex = 0;
   this.label1.Text = "label1";

   this.label2.Location = new System.Drawing.Point(48, 64);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(264, 24);
   this.label2.TabIndex = 1;
   this.label2.Text = "label2";

   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(376, 221);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label2,this.label1});
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);
  }
  #endregion
  static void Main()
  {
   Application.Run(new Form1());
  }
  private static string getIpAddress()
  {
   System.Net.IPAddress addr;
   addr=new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address) ;
   return addr.ToString();
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.