How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#
Just made this in a few minutes:
using System;
using System.Management;
namespace WindowsFormsApplication_CS
{
class NetworkManagement
{
public void setIP(string ip_address, string subnet_mask)
{
ManagementClass objMC =
new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
ManagementBaseObject setIP;
ManagementBaseObject newIP =
objMO.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ip_address };
newIP["SubnetMask"] = new string[] { subnet_mask };
setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
}
}
}
public void setGateway(string gateway)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
ManagementBaseObject setGateway;
ManagementBaseObject newGateway =
objMO.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = new string[] { gateway };
newGateway["GatewayCostMetric"] = new int[] { 1 };
setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);
}
}
}
public void setDNS(string NIC, string DNS)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
// if you are using the System.Net.NetworkInformation.NetworkInterface
// you'll need to change this line to
// if (objMO["Caption"].ToString().Contains(NIC))
// and pass in the Description property instead of the name
if (objMO["Caption"].Equals(NIC))
{
ManagementBaseObject newDNS =
objMO.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = DNS.Split(',');
ManagementBaseObject setDNS =
objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
}
}
}
}
public void setWINS(string NIC, string priWINS, string secWINS)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
if (objMO["Caption"].Equals(NIC))
{
ManagementBaseObject setWINS;
ManagementBaseObject wins =
objMO.GetMethodParameters("SetWINSServer");
wins.SetPropertyValue("WINSPrimaryServer", priWINS);
wins.SetPropertyValue("WINSSecondaryServer", secWINS);
setWINS = objMO.InvokeMethod("SetWINSServer", wins, null);
}
}
}
}
}
}
Trying to change the network settings(DNS) of a network adapter in c# with Win32_NetworkAdapterConfiguration but I can't see what I'm doing wrong
Yep, running my app elevated through app.manifest
configuration fixed it for me. All I needed was elevated permissions. Thanks for help!
Change IP address, subnet mask, dns servers and default gateway in C#
I have decided to go another route with this. Instead of using C#, I'm simply going to set it via a batch file.
set_static_ip.bat
netsh interface ip set address name="Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1
netsh interface ip set dnsservers "Local Area Connection" static 192.168.0.3 primary
The first line sets the ip and default gateway.
The second line sets the primary dns server. I haven't been able to set the alternate dns, but i'm sure i'm just missing something.
If you want to change the configuration back to obtain an ip address automatically, then you simply run the code below.
set_auto_ip.bat
netsh interface ip set address name="Local Area Connection" source="dhcp"
netsh interface ip set dnsservers name="Local Area Connection" source=dhcp
Simple yet effective.
Related Topics
Routing with Multiple Parameters Using ASP.NET MVC
Load an Exe File and Run It from Memory
Open Existing File, Append a Single Line
Can Anonymous Class Implement Interface
Parallel.Foreach and Async-Await
How to Make Something That Catches All 'Unhandled' Exceptions in a Winforms Application
Differencebetween Task.Run() and Task.Factory.Startnew()
Puzzling Enumerable.Cast Invalidcastexception
How Might I Schedule a C# Windows Service to Perform a Task Daily
Execute Task in Background in Wpf Application
How to Get Object Size in Memory
How to Abort a Task Like Aborting a Thread (Thread.Abort Method)
How to Bind a List to a Combobox
How to Change Datatype of a Datacolumn in a Datatable
Cross-Thread Operation Not Valid
Ignoring a Class Property in Entity Framework 4.1 Code First
Copy File(S) from One Project to Another Using Post Build Event...Vs2010