登录 注册
当前位置:主页 > 资源下载 > 10 > 用C#编程来更改IP地址、默认网关和DNS设置

用C#编程来更改IP地址、默认网关和DNS设置

  • 更新:2024-12-04 17:41:56
  • 大小:30KB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:C# - 后端
  • 格式:TXT

资源介绍

、在 “解决方案资源管理器” 右击 “引用” 添加 “System.Management” 的引用。 2、添加 “using System.Management;”。 static void SetNetworkAdapter() { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (!(bool)mo["IPEnabled"]) continue; //设置ip地址和子网掩码 inPar = mo.GetMethodParameters("EnableStatic"); inPar["IPAddress"] = new string[] { "192.168.16.248", "192.168.16.249" };// 1.备用 2.IP inPar["SubnetMask"] = new string[] { "255.255.255.0", "255.255.255.0" }; outPar = mo.InvokeMethod("EnableStatic", inPar, null); //设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); inPar["DefaultIPGateway"] = new string[] { "192.168.16.2", "192.168.16.254" }; // 1.网关;2.备用网关 outPar = mo.InvokeMethod("SetGateways", inPar, null); //设置DNS inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar["DNSServerSearchOrder"] = new string[] { "211.97.168.129", "202.102.152.3" }; // 1.DNS 2.备用DNS outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); break; } } C# 开机自动运行程序2007-12-31 12:32string StartupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); System.IO.File.Copy("应用程序路径(包括程序名)", StartupPath + "执行程序文件名称", true); . . . . . . . . .