c# 指定软件运行时自动设置指定WiFi或有线网卡的IPv4网段,用MahApps.Metro来获取输入的IP和网卡名,并可以保存多个,以便下次选择使用
时间: 2024-03-28 16:36:44 浏览: 60
好的,您可以按照以下步骤来实现这个功能:
1. 添加MahApps.Metro的NuGet包,并在代码中引用。
```csharp
using MahApps.Metro.Controls;
```
2. 创建一个MainWindow窗口,并在窗口中添加一个ComboBox控件和两个TextBox控件。ComboBox控件用于选择网络适配器,TextBox控件用于输入IPv4网段。
```csharp
<Controls:MetroWindow x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="comboBoxAdapters" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="250" SelectionChanged="comboBoxAdapters_SelectionChanged"/>
<TextBox x:Name="textBoxIP" HorizontalAlignment="Left" Margin="10,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250"/>
<TextBox x:Name="textBoxName" HorizontalAlignment="Left" Margin="10,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250"/>
</Grid>
</Controls:MetroWindow>
```
3. 在窗口的构造函数中,通过WMI查询获取所有的网络适配器,并将它们添加到ComboBox控件中。
```csharp
public MainWindow()
{
InitializeComponent();
// 获取所有的网络适配器
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
foreach (ManagementObject obj in searcher.Get())
{
string adapterName = obj["Description"].ToString();
comboBoxAdapters.Items.Add(adapterName);
}
}
```
4. 在ComboBox的SelectionChanged事件中,获取所选的网络适配器,并将其保存到变量中。同时,从变量中获取IPv4地址和子网掩码,并将其填充到TextBox中。
```csharp
private ManagementObject selectedAdapter;
private void comboBoxAdapters_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 获取所选的网络适配器
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
foreach (ManagementObject obj in searcher.Get())
{
string adapterName = obj["Description"].ToString();
if (adapterName == comboBoxAdapters.SelectedItem.ToString())
{
selectedAdapter = obj;
// 获取IPv4地址和子网掩码
string[] addresses = (string[])obj["IPAddress"];
string[] subnetMasks = (string[])obj["IPSubnet"];
if (addresses.Length > 0 && subnetMasks.Length > 0)
{
textBoxIP.Text = addresses[0] + "/" + GetSubnetMaskLength(subnetMasks[0]);
}
}
}
}
private int GetSubnetMaskLength(string subnetMask)
{
string[] octets = subnetMask.Split('.');
int length = 0;
foreach (string octet in octets)
{
byte b = byte.Parse(octet);
while (b > 0)
{
length++;
b <<= 1;
}
}
return length;
}
```
5. 在窗口中添加一个Button控件,用于保存所选的IPv4网段和网络适配器名称。在Button的Click事件中,将所选的IPv4网段和网络适配器名称保存到本地配置文件中。
```csharp
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
if (textBoxIP.Text != "" && textBoxName.Text != "" && selectedAdapter != null)
{
// 保存IPv4网段和网络适配器名称到本地配置文件中
Properties.Settings.Default.IPAddress = textBoxIP.Text;
Properties.Settings.Default.AdapterName = selectedAdapter["Description"].ToString();
Properties.Settings.Default.Save();
// 清空输入框
textBoxIP.Text = "";
textBoxName.Text = "";
}
}
```
6. 在窗口的Loaded事件中,从本地配置文件中加载所有保存的IPv4网段和网络适配器名称,并将它们添加到ComboBox控件中。
```csharp
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
// 从本地配置文件中加载所有保存的IPv4网段和网络适配器名称
string[] ipAddresses = Properties.Settings.Default.IPAddresses.Split(',');
string[] adapterNames = Properties.Settings.Default.AdapterNames.Split(',');
for (int i = 0; i < ipAddresses.Length && i < adapterNames.Length; i++)
{
comboBoxSaved.Items.Add(new SavedAdapter(ipAddresses[i], adapterNames[i]));
}
}
```
7. 在窗口中添加另一个ComboBox控件,用于选择保存的IPv4网段和网络适配器名称。在ComboBox的SelectionChanged事件中,将所选的IPv4网段和网络适配器名称填充到TextBox中。
```csharp
private void comboBoxSaved_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxSaved.SelectedItem != null)
{
SavedAdapter savedAdapter = (SavedAdapter)comboBoxSaved.SelectedItem;
textBoxIP.Text = savedAdapter.IPAddress;
comboBoxAdapters.SelectedItem = savedAdapter.AdapterName;
}
}
```
8. 最后,您可以在程序中使用所选的IPv4网段和网络适配器名称来设置指定的WiFi或有线网卡的IPv4网段。
完整的代码示例如下:
阅读全文