C#使用cmd更改IP地址

使用NetworkInterface.GetAllNetworkInterfaces获取所有除环回地址跟隧道适配器地址的地址,并显示在Listbox上,通过更改listBox1_MouseDoubleClick事件,如果listBox1.SelectedIndex的值不为-1,即有选中项时,遍历所有的地址,直到找到与选中项名字相同的为止,并将适配器信息传递给新窗体。

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.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

namespace 修改IP地址
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            NetworkInterface[] NwIfs = NetworkInterface.GetAllNetworkInterfaces();
            foreach(NetworkInterface  NwIf in NwIfs)
            {
                if (NwIf.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                    continue;
                if (NwIf.NetworkInterfaceType == NetworkInterfaceType.Tunnel)
                    continue;
                listBox1.Items.Add(NwIf.Name);
            }
        }
        private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                NetworkInterface[] NwIfs = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface NwIf in NwIfs)
                {
                    if (NwIf.Name == listBox1.SelectedItem.ToString())
                    {
                        IP Wd = new IP(NwIf);
                        Wd.Show();
                    }
                }
            }
        }
    }
}

在新窗体中声明三个变量,记录改变地址前的数据,并使用TextBox显示出IP地址,子网掩码和默认网关。通过实例化process对象,打开cmd.exe,并输入命令行,格式为 netsh interface ip set address “本地连接 2” static ipv4 ipv4msk gateway* , 如果成功修改,则更新三个变量为当前IP地址,如果修改失败,则输出失败的结果,并通过三个变量将TextBox的值复原。

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.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Threading;

namespace 修改IP地址
{
    /// 
    /// IP.xaml 的交互逻辑
    /// 
    public partial class IP : Window
    {
        string Tx1, Tx2, Tx3;
        private NetworkInterface Nwif;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Title = Nwif.Name.ToString();
            IPInterfaceProperties Propers = Nwif.GetIPProperties();
            foreach (UnicastIPAddressInformation UipI in Propers.UnicastAddresses)
            {
                if (UipI.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    textBox1.Text = UipI.Address.ToString();
                    textBox2.Text = UipI.IPv4Mask.ToString();
                    Tx1 = UipI.Address.ToString();
                    Tx2 = UipI.IPv4Mask.ToString();
                }
            }
            try
            {
                textBox3.Text = Propers.GatewayAddresses[0].Address.ToString();
                Tx3 = Propers.GatewayAddresses[0].Address.ToString();
            }
            catch
            {
                textBox3.Text = "";
            }
        }

        public IP(NetworkInterface Nwif)
        {
            InitializeComponent();
            this.Nwif = Nwif;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;  //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true; //重定向标准输入
            p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
            p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
            p.StartInfo.CreateNoWindow = true; //不显示程序窗口
            Thread.Sleep(1000);
            p.Start();
            string S = "netsh interface ip set address " + "\"" + Title + "\"" + " static " + textBox1.Text + " " + textBox2.Text + " " + textBox3.Text + " " + "&exit";
            // 格式 netsh interface ip set address "本地连接* 2" static ipv4 ipv4msk gateway 
            p.StandardInput.WriteLine(S);
            S = p.StandardOutput.ReadLine();
            S = p.StandardOutput.ReadLine();
            S = p.StandardOutput.ReadLine();
            S = p.StandardOutput.ReadLine();
            S = p.StandardOutput.ReadLine();
            S = p.StandardOutput.ReadLine();
            if (S == null)
            {
                MessageBox.Show("Accept", "Success");
                Tx1 = textBox1.Text;
                Tx2 = textBox2.Text;
                Tx3 = textBox3.Text;
            }
            else
            {
                MessageBox.Show(S.ToString(), "Warning");
                textBox1.Text = Tx1;
                textBox2.Text = Tx2;
                textBox3.Text = Tx3;
            }
        }
    }
}

在写这个的过程中,因为少写了p.StandardOutput.ReadLine(); 以及输错命令行,导致出现了很多问题。以后对于这方面要加强警惕。

你可能感兴趣的:(C#菜鸡之路)