Introduction
C# can change network settings like IP address, DNS servers, WINS servers, and the computer hostname using Windows Management Instrumentation (WMI) through the System.Management namespace, or by executing netsh commands. WMI provides programmatic access to the Win32_NetworkAdapterConfiguration class, which exposes methods for setting static IPs, DNS, and gateway addresses. These operations require administrator privileges. For .NET Core/.NET 5+ cross-platform scenarios, Process.Start("netsh", ...) or PowerShell commands are the practical alternative.
Prerequisites
All network configuration changes require running the application as Administrator. Add an application manifest to request elevation:
<!-- app.manifest -->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Add the System.Management NuGet package:
dotnet add package System.Management
Setting a Static IP Address
1using System.Management;
2
3public static void SetStaticIP(string adapterName, string ipAddress,
4 string subnetMask, string gateway)
5{
6 var query = new ManagementObjectSearcher(
7 "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE");
8
9 foreach (ManagementObject adapter in query.Get())
10 {
11 if (adapter["Description"].ToString() == adapterName)
12 {
13 // Set IP address and subnet mask
14 var ipParams = adapter.GetMethodParameters("EnableStatic");
15 ipParams["IPAddress"] = new string[] { ipAddress };
16 ipParams["SubnetMask"] = new string[] { subnetMask };
17 adapter.InvokeMethod("EnableStatic", ipParams, null);
18
19 // Set default gateway
20 var gwParams = adapter.GetMethodParameters("SetGateways");
21 gwParams["DefaultIPGateway"] = new string[] { gateway };
22 gwParams["GatewayCostMetric"] = new int[] { 1 };
23 adapter.InvokeMethod("SetGateways", gwParams, null);
24
25 Console.WriteLine($"Set IP {ipAddress} on {adapterName}");
26 break;
27 }
28 }
29}
30
31// Usage
32SetStaticIP("Intel(R) Ethernet Connection", "192.168.1.100",
33 "255.255.255.0", "192.168.1.1");
Setting DNS Servers
1public static void SetDNS(string adapterName, string primaryDns, string secondaryDns)
2{
3 var query = new ManagementObjectSearcher(
4 "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE");
5
6 foreach (ManagementObject adapter in query.Get())
7 {
8 if (adapter["Description"].ToString() == adapterName)
9 {
10 var dnsParams = adapter.GetMethodParameters("SetDNSServerSearchOrder");
11 dnsParams["DNSServerSearchOrder"] = new string[] { primaryDns, secondaryDns };
12 adapter.InvokeMethod("SetDNSServerSearchOrder", dnsParams, null);
13
14 Console.WriteLine($"DNS set to {primaryDns}, {secondaryDns}");
15 break;
16 }
17 }
18}
19
20// Set Google DNS
21SetDNS("Intel(R) Ethernet Connection", "8.8.8.8", "8.8.4.4");
Setting WINS Servers
1public static void SetWINS(string adapterName, string primaryWins, string secondaryWins)
2{
3 var query = new ManagementObjectSearcher(
4 "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE");
5
6 foreach (ManagementObject adapter in query.Get())
7 {
8 if (adapter["Description"].ToString() == adapterName)
9 {
10 var winsParams = adapter.GetMethodParameters("SetWINSServer");
11 winsParams["WINSPrimaryServer"] = primaryWins;
12 winsParams["WINSSecondaryServer"] = secondaryWins;
13 adapter.InvokeMethod("SetWINSServer", winsParams, null);
14
15 Console.WriteLine("WINS servers configured");
16 break;
17 }
18 }
19}
20
21SetWINS("Intel(R) Ethernet Connection", "192.168.1.10", "192.168.1.11");
Changing the Computer Host Name
1public static void SetHostName(string newName)
2{
3 var computer = new ManagementObject(
4 $"Win32_ComputerSystem.Name='{Environment.MachineName}'");
5 var renameParams = computer.GetMethodParameters("Rename");
6 renameParams["Name"] = newName;
7
8 var result = computer.InvokeMethod("Rename", renameParams, null);
9 uint returnValue = (uint)result["ReturnValue"];
10
11 if (returnValue == 0)
12 Console.WriteLine($"Hostname changed to {newName}. Restart required.");
13 else
14 Console.WriteLine($"Failed to rename. Error code: {returnValue}");
15}
16
17SetHostName("WORKSTATION-01");
The hostname change takes effect after a system restart.
Switching to DHCP
1public static void EnableDHCP(string adapterName)
2{
3 var query = new ManagementObjectSearcher(
4 "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE");
5
6 foreach (ManagementObject adapter in query.Get())
7 {
8 if (adapter["Description"].ToString() == adapterName)
9 {
10 // Enable DHCP for IP
11 adapter.InvokeMethod("EnableDHCP", null);
12
13 // Enable DHCP for DNS
14 adapter.InvokeMethod("SetDNSServerSearchOrder", null);
15
16 Console.WriteLine("DHCP enabled");
17 break;
18 }
19 }
20}
Alternative: Using netsh Commands
For simpler implementations or cross-platform .NET, use netsh:
1using System.Diagnostics;
2
3public static void RunNetsh(string arguments)
4{
5 var process = new Process
6 {
7 StartInfo = new ProcessStartInfo
8 {
9 FileName = "netsh",
10 Arguments = arguments,
11 RedirectStandardOutput = true,
12 UseShellExecute = false,
13 CreateNoWindow = true
14 }
15 };
16 process.Start();
17 string output = process.StandardOutput.ReadToEnd();
18 process.WaitForExit();
19 Console.WriteLine(output);
20}
21
22// Set static IP
23RunNetsh("interface ip set address \"Ethernet\" static 192.168.1.100 255.255.255.0 192.168.1.1");
24
25// Set DNS
26RunNetsh("interface ip set dns \"Ethernet\" static 8.8.8.8 primary");
27RunNetsh("interface ip add dns \"Ethernet\" 8.8.4.4 index=2");
28
29// Switch to DHCP
30RunNetsh("interface ip set address \"Ethernet\" dhcp");
31RunNetsh("interface ip set dns \"Ethernet\" dhcp");
Reading Current Network Settings
1public static void GetNetworkInfo()
2{
3 var query = new ManagementObjectSearcher(
4 "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE");
5
6 foreach (ManagementObject adapter in query.Get())
7 {
8 Console.WriteLine($"Adapter: {adapter["Description"]}");
9 Console.WriteLine($" DHCP Enabled: {adapter["DHCPEnabled"]}");
10
11 var ips = adapter["IPAddress"] as string[];
12 if (ips != null)
13 Console.WriteLine($" IP Address: {string.Join(", ", ips)}");
14
15 var dns = adapter["DNSServerSearchOrder"] as string[];
16 if (dns != null)
17 Console.WriteLine($" DNS Servers: {string.Join(", ", dns)}");
18
19 Console.WriteLine($" MAC Address: {adapter["MACAddress"]}");
20 Console.WriteLine();
21 }
22}
Common Pitfalls
Not running as Administrator: WMI network configuration methods fail silently or throw ManagementException without admin privileges. Always run the application elevated and include an app manifest requesting requireAdministrator.
Using the wrong adapter name: The Description property must match the exact adapter name shown in Device Manager, not the connection name from Network Connections. Use GetNetworkInfo() to list available adapter descriptions.
WMI not available on .NET Core Linux/macOS: System.Management and WMI are Windows-only. For cross-platform network configuration, use platform-specific commands (netsh on Windows, ip on Linux, networksetup on macOS) via Process.Start().
Forgetting to restart after hostname change: Win32_ComputerSystem.Rename() changes the hostname in the registry but does not take effect until the computer restarts.
Setting DNS without clearing old entries first: Calling SetDNSServerSearchOrder replaces the entire list. If you need to add a DNS server without removing existing ones, read the current list first, append the new entry, and set the combined array.
Summary
Use Win32_NetworkAdapterConfiguration via WMI to set static IP, DNS, WINS, and gateway programmatically
Change the computer hostname with Win32_ComputerSystem.Rename() (requires restart)
All network configuration changes require running as Administrator
Use netsh commands via Process.Start() as a simpler alternative, especially for .NET Core
Always read current settings first with a WMI query to identify the correct adapter description