C# 如何设置网络适配器的IP地址?
为了玩局域网游戏和在家里上网,我必须经常更换IP地址。我正在用C语言创建一个应用程序,它可以快速完成。我创建了一些字段,如适配器名称、IP地址、子网、DNS服务器地址。 我在设置IP按钮上运行的代码如下:C# 如何设置网络适配器的IP地址?,c#,winforms,visual-studio,process,ip-address,C#,Winforms,Visual Studio,Process,Ip Address,为了玩局域网游戏和在家里上网,我必须经常更换IP地址。我正在用C语言创建一个应用程序,它可以快速完成。我创建了一些字段,如适配器名称、IP地址、子网、DNS服务器地址。 我在设置IP按钮上运行的代码如下: string adapter = comboAdapterName.Text; string ip = comboIPAddress.Text; string subnet = comboSubnet.Text; string dns = comboDNS.Text; 现在我想使用这个处理方
string adapter = comboAdapterName.Text;
string ip = comboIPAddress.Text;
string subnet = comboSubnet.Text;
string dns = comboDNS.Text;
现在我想使用这个处理方法从这些字段中获取数据,并相应地追加字符串
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", "interface ip set address \"Local Area Connection\" static 192.168.0.10 255.255.255.0 192.168.0.1 1");
p.StartInfo = psi;
p.Start();
但我想这并不容易。因为我无法在不影响格式的情况下编辑此文件。我还尝试使用许多+s创建一个全新的字符串,我可以将其放置为:
ProcessStartInfo psi = new ProcessStartInfo(mystring);
但这对我来说还是太难了。请建议一个简单的方法来做到这一点
==========================================================================
我想我明白了:
string ipstring = "netsh interface ip set address " + "\"" + adapter + "\"" + " " + "static" + " " + ip + " " + subnet + " " + dns;
您将需要使用该方法
例如:
string subnet = comboSubnet.Text;
string formatted = string.Format("Subnet is: {0}", subnet);
MessageBox.Show(formatted);
将该字符串格式化为您想要的格式 您可以通过以下功能获取当前适配器配置:
private static void EthernetInf(out string ip, out string dns, out string nic) // To get current ethernet config
{
ip = "";
dns = "";
nic = "";
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
foreach (IPAddress dnsAdress in ni.GetIPProperties().DnsAddresses)
{
if (dnsAdress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
dns = dnsAdress.ToString();
}
}
foreach (UnicastIPAddressInformation ips in ni.GetIPProperties().UnicastAddresses)
{
if (ips.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !ips.Address.ToString().StartsWith("169")) //to exclude automatic ips
{
ip = ips.Address.ToString();
nic = ni.Name;
}
}
}
}
以下功能用于在提升的命令提示符中设置IP:
private void SetIP(Button sender, string arg) //To set IP with elevated cmd prompt
{
try
{
if (sender.Background == Brushes.Cyan )
{
MessageBox.Show("Already Selected...");
return;
}
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.UseShellExecute = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.Verb = "runas";
psi.Arguments = arg;
Process.Start(psi);
if (sender == EthStatic || sender == EthDHCP )
{
EthStatic.ClearValue(Button.BackgroundProperty);
EthDHCP.ClearValue(Button.BackgroundProperty);
sender.Background = Brushes.Cyan;
}
if (sender == WIFIStatic || sender == WIFIDhcp)
{
WIFIStatic.ClearValue(Button.BackgroundProperty);
WIFIDhcp.ClearValue(Button.BackgroundProperty);
sender.Background = Brushes.Cyan;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
此单击按钮代码将参数传递给processstartinfo以设置IP
private void EthStatic_Click(object sender, RoutedEventArgs e)
{
SetIP(EthStatic, "/c netsh interface ip set address \"" + EthName + "\" static " + Properties.Settings.Default.EthIPac + " " + Properties.Settings.Default.Subnet + " " + Properties.Settings.Default.EthDnsac + " & netsh interface ip set dns \"" + EthName + "\" static " + Properties.Settings.Default.EthDnsac);
}
完整的应用程序可从以下网址获得:
以什么方式?海报上要求把线连起来,说这对他来说太难了。他不能用他已有的部分来建造他的绳子。Format是一种从已有部分构建字符串的方法。实际上,我无法添加字符串。真是愚蠢。谢谢你抽出时间。