C#使用cmd更改IP地址
来源:互联网 发布:外貌协会的女生知乎 编辑:程序博客网 时间:2024/06/04 19:52
使用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地址{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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地址{ /// <summary> /// IP.xaml 的交互逻辑 /// </summary> 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(); 以及输错命令行,导致出现了很多问题。以后对于这方面要加强警惕。
0 0
- C#使用cmd更改IP地址
- CMD命令更改IP地址
- 使用WMI更改IP地址
- 使用WMI更改IP地址
- cmd 如何更改IP
- CMD更改IP设置
- 使用bat文件更改IP地址
- CMD修改IP地址
- 更改主机IP地址
- IP地址更改助手
- 更改IP地址
- 批处理更改 ip地址
- Centos 更改IP地址
- 使用模拟CMD命令方式修改IP地址以及网关
- ESX-使用命令更改Service Console的IP地址
- CMD下修改IP地址!
- 更改ip地址的命令
- Solaris下更改IP地址
- 判断有向图中是否存在环
- caffe小显存问题
- 蓝桥杯之分解质因数
- (ssl1632)P1282 多米诺骨牌
- Unity3d——GFootball_03:镜头跟随球员
- C#使用cmd更改IP地址
- 【SPOJ705】New Distinct Substrings 后缀数组
- DL学习笔记【17】nn包中的各位Convolutional layers
- SSH学习之Session接口的常用方法
- JAVA中创建对象的四种方式
- "Could not resolve placeholder"解决方案
- 腾讯EC .net API对接第三方系统
- 【Jenkins】——Jenkins+jmeter 定时测试
- 苹果-SSL 1637