In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to set local network DNS, gateway, subnet mask, IP and so on in C#". In daily operation, I believe many people have doubts about how to set local network DNS, gateway, subnet mask, IP and so on. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to set local network DNS, gateway, subnet mask, IP, etc." Next, please follow the editor to study!
This Demo is managed through the "Win32_NetworkAdapterConfiguration" class. This basically includes the setting information of IP,DNS and gateway.
Using WMI in C # is relatively simple:
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Text
Using System
Using System.Collections
Using System.Text
Using System.Management
Using System.Text.RegularExpressions
Namespace Demo
{
/ / /
/ / Network settings class, which sets various network parameters (DNS, gateway, subnet mask, IP)
/ / /
Public class NetworkSetting
{
Public NetworkSetting ()
{
/ / Constructor logic
}
/ / /
/ set DNS
/ / /
/ / /
Public static void SetDNS (string [] dns)
{
SetIPAddress (null, dns)
}
/ / /
/ set the gateway
/ / /
/ / /
Public static void SetGetWay (string getway)
{
SetIPAddress (null, null, new string [] {getway}, null)
}
/ / /
/ set the gateway
/ / /
/ / /
Public static void SetGetWay (string [] getway)
{
SetIPAddress (null, null, getway, null)
}
/ / /
/ / set IP address and mask
/ / /
/ / /
/ / /
Public static void SetIPAddress (string ip, string submask)
{
SetIPAddress (new string [] {ip}, new string [] {submask}, null, null)
}
/ / /
/ / set IP address, mask and gateway
/ / /
/ / /
/ / /
/ / /
Public static void SetIPAddress (string ip, string submask, string getway)
{
SetIPAddress (new string [] {ip}, new string [] {submask}, new string [] {getway}, null)
}
/ / /
/ / set IP address, mask, gateway and DNS
/ / /
/ / /
/ / /
/ / /
/ / /
Public static void SetIPAddress (string [] ip, string [] submask, string [] getway, string [] dns)
{
ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration")
ManagementObjectCollection moc = wmi.GetInstances ()
ManagementBaseObject inPar = null
ManagementBaseObject outPar = null
Foreach (ManagementObject mo in moc)
{
/ / skip if there are no network devices with IP settings enabled
If (! (bool) mo ["IPEnabled"])
Continue
/ / set IP address and mask
If (ip! = null & & submask! = null)
{
InPar = mo.GetMethodParameters ("EnableStatic")
InPar ["IPAddress"] = ip
InPar ["SubnetMask"] = submask
OutPar = mo.InvokeMethod ("EnableStatic", inPar, null)
}
/ / set the gateway address
If (getway! = null)
{
InPar = mo.GetMethodParameters ("SetGateways")
InPar ["DefaultIPGateway"] = getway
OutPar = mo.InvokeMethod ("SetGateways", inPar, null)
}
/ / set the DNS address
If (dns! = null)
{
InPar = mo.GetMethodParameters ("SetDNSServerSearchOrder")
InPar ["DNSServerSearchOrder"] = dns
OutPar = mo.InvokeMethod ("SetDNSServerSearchOrder", inPar, null)
}
}
}
/ / /
/ / enable DHCP server
/ / /
Public static void EnableDHCP ()
{
ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration")
ManagementObjectCollection moc = wmi.GetInstances ()
Foreach (ManagementObject mo in moc)
{
/ / skip if there are no network devices with IP settings enabled
If (! (bool) mo ["IPEnabled"])
Continue
/ / reset DNS to be empty
Mo.InvokeMethod ("SetDNSServerSearchOrder", null)
/ / enable DHCP
Mo.InvokeMethod ("EnableDHCP", null)
}
}
/ / /
/ determine whether it conforms to the IP address format
/ / /
/ / /
/ / /
Public static bool IsIPAddress (string ip)
{
/ / complete IP with "." Group boundaries
String [] arr = ip.Split ('.')
/ / determine whether IP is composed of four sets of numbers
If (arr.Length! = 4)
Return false
/ / regular expression, 1 to 3-bit integer
String pattern = @ "\ d {1pr 3}"
For (int I = 0; I
< arr.Length; i++) { string d = arr[i]; //判断IP开头是否为0 if (i == 0 && d == "0") return false; //判断IP是否是由1~3位数组成 if (!Regex.IsMatch(d, pattern)) return false; if (d != "0") { //判断IP的每组数是否全为0 d = d.TrimStart('0'); if (d == "") return false; //判断IP每组数是否大于255 if (int.Parse(d) >255)
Return false
}
} return true
}
}
}
At this point, the study on "how to set local network DNS, gateway, subnet mask, IP, etc." in C# is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.