C#如何获得对方机器的名称和IP地址?

C#如何获得对方机器的名称和IP地址?

  1. 获取计算机名称和本机固定的IP地址。代码和详细说明如下:
    ip001.cs
    //导入程序用到的名称空间
    using System ;
    using System.Net;
    using System.Windows.Forms ;
    using System.Drawing ;
    public class Form3 : Form
    {
    //定义二个标签
    private Label label1 ;
    private Label label2 ;
    public static void Main ( )
    {
    Application.Run ( new Form3 ( ) ) ;
    }
    // 构造窗体
    public Form3 ( )
    {
    // 建立标签并且初始化
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    //先继承一个Label 类
    label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
    label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
    //设定 Label的显示位置
    label1.Text = “主机名称:” + System.Net.Dns.GetHostName ( ) ;
    // 显示本机的计算机名称. GetHostName ( ) 是名称空间System.Net中类Dns中定义的一个重要方法,其返回值getIPAddress ( )就是本地计算机名称
    label2.Text = “IP 地址:” + getIPAddress ( ) ;
    // 显示本机的局域网IP地址
    label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
    label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
    //设定标签的大小
    label1.TabIndex = 0 ;
    label2.TabIndex = 1 ;
    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    // 设定标签的对齐方式
    this.Text = “获得主机名称和IP地址.” ;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
    // 设定窗体的边界类型
    this.ForeColor = System.Drawing.SystemColors.Desktop ;
    this.Font = new System.Drawing.Font ( “宋体” , 10 , System.Drawing.FontStyle.Bold ) ;
    // 设定字体、大小就字体的式样
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
    this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
    //把标签加到窗体中
    this.Controls.Add ( this.label1 ) ;
    this.Controls.Add ( this.label2 ) ;
    }
    private static string getIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得本机局域网IP地址. IPHostEntry 对象的属性 AddressList是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息.在这儿AddressList [0].Address是固定的IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
    return addr.ToString ( ) ;
    }
    }

编译:
csc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /t:winexeip001.cs
我们就可以用得到的这个ip001.exe文件来读取本地固定的IP地址了。

  1. 获取计算机名称和态IP地址。代码和详细说明如下:
    ip002.cs
    //导入程序用到的名称空间
    using System ;
    using System.Net;
    using System.Windows.Forms ;
    using System.Drawing ;
    public class Form3 : Form
    {
    //定义二个标签
    private Label label1 ;
    private Label label2 ;
    public static void Main ( )
    {
    Application.Run ( new Form3 ( ) ) ;
    }
    // 构造窗体
    public Form3 ( )
    {
    // 建立标签并且初始化
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    //先继承一个Label 类
    label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
    label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
    //设定 Label的显示位置
    label1.Text = “主机名称:” + System.Net.Dns.GetHostName ( ) ;
    // 显示本机的计算机名称
    label2.Text = “IP 地址:” + getIPAddress ( ) ;
    // 显示本机的拨号动态分配IP地址
    label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
    label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
    //设定标签的大小
    label1.TabIndex = 0 ;
    label2.TabIndex = 1 ;
    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    // 设定标签的对齐方式
    this.Text = “获得主机名称和IP地址.” ;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
    // 设定窗体的边界类型
    this.ForeColor = System.Drawing.SystemColors.Desktop ;
    this.Font = new System.Drawing.Font ( “宋体” , 10 , System.Drawing.FontStyle.Bold ) ;
    // 设定字体、大小就字体的式样
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
    this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
    //把标签加到窗体中
    this.Controls.Add ( this.label1 ) ;
    this.Controls.Add ( this.label2 ) ;
    }
    private static string getIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得拨号动态分配IP地址. GetHostByName ( )方法返回值时IPHostEntry 对象属性 AddressList是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息.在这儿.AddressList .Address是上网动态分配的IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [1].Address ) ;
    return addr.ToString ( ) ;
    }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毕业设计方案专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值