网络编程技术2021-09-03之输出本机IP地址

本文介绍了如何在Windows应用程序中获取本机的IPv4和IPv6地址,并展示了使用StringBuilder提高代码效率的方法。通过Dns.GetHostAddresses和不同地址家族检查,实现IPv4和IPv6的筛选输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输出本机的IP地址并加以筛选ipv4型、ipv6型

1.文件MainWindow.xaml

<Window x:Class="w3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:w3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Center" Margin="0,360,0,0" VerticalAlignment="Top" Height="30" Width="57" Click="Button_Click"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="223,82,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="367" Height="231"/>

    </Grid>
</Window>

2.文件MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
namespace w3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string hostName = Dns.GetHostName();
            this.textBox.Text = hostName;
            //IPAddress[] ips = Dns.GetHostAddresses("127.0.0.1");//只能返回双引号里面的一个地址
            IPAddress[] ips = Dns.GetHostAddresses(hostName);
            int count = ips.Length;
            int i = 0;
            while (i < count)
            {
                IPAddress ip = ips[i];
                if (ip.AddressFamily == AddressFamily.InterNetwork) {
                 this.textBox.AppendText(i+"\n"+ip.ToString());//筛选出ipv4型的地址输出
                }
                i++;
            }
            Console.ReadLine();
        }
    }
}

效果:
在这里插入图片描述
3.进阶版文件MainWindow.xaml.cs

使用StringBuilder效率更高。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
namespace w3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string hostName = Dns.GetHostName();
            sb.AppendLine("主机名:"+hostName);
            //IPAddress[] ips = Dns.GetHostAddresses("127.0.0.1");//只能返回双引号里面的一个地址
            IPAddress[] ips = Dns.GetHostAddresses(hostName);
            int count = ips.Length;
            int i = 0;
            while (i < count)
            {
                IPAddress ip = ips[i];
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    sb.AppendLine("ipv4:" + ip.ToString());
                }
                else { 
                    sb.AppendLine("ipv6:" + ip.ToString());
                }
                i++;
            }
            this.textBox.Text = sb.ToString();
            Console.ReadLine();
        }
    }
}

效果:

在这里插入图片描述4.进阶版文件MainWindow.xaml.cs

获取本机IP地址的其它的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
namespace w3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string hostName = Dns.GetHostName();
            IPHostEntry ipe = Dns.GetHostEntry(hostName);//注意此处不能使用IPHostEntry ipe = Dns.GetHostByName(hostName);//被弃用了
            IPAddress[] ips = ipe.AddressList;
            sb.AppendLine("主机名:"+ipe.HostName);

            foreach(IPAddress localIP in ipe.AddressList) { 
                if (localIP.AddressFamily == AddressFamily.InterNetwork)
                {
                    sb.AppendLine("ipv4:" + localIP.ToString());
                }
                else
                {
                    sb.AppendLine("ipv6:" + localIP.ToString());
                }
            }

            this.textBox.Text = sb.ToString();
            Console.ReadLine();
        }
    }
}

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值