资源简介
C#实现的自动更改电脑IP,省去繁琐的操作。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace SetIP
{
class Program
{
static void Main(string[] args)
{
// string IP;
// IP = Console.ReadLine();
// SetNetworkAdapter(IP);
EnableDHCP();
//ShowNetworkAdapterInfo();
//Console.ReadKey();
}
static void SetNetworkAdapter(string IP)
{
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[] { IP “192.168.1.5“ };// 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;
}
}
public static void EnableDHCP()
{
ManagementClass wmi = new ManagementClass(“Win32_NetworkAdapterConfiguration“);
ManagementobjectCollection moc = wmi.GetInstances();
foreach (Managementobject mo in moc)
{
//如果没有启用IP设置的网络设备则跳过
if (!(bool)mo[“IPEnabled“])
continue;
//重置DNS为空
mo.InvokeMethod(“SetDNSServerSearchOrder“ null);
//开启DHCP
mo.InvokeMethod(“EnableDHCP“ null);
}
}
static void ShowNetworkAdapterInfo()
{
Console.WriteLine(“****** Current Network Adapter Information ******“);
ManagementClass mc = new ManagementClass(“Win32_NetworkAdapterConfiguration“);
ManagementobjectCollection moc = mc.GetInstances();
foreach (Managementobject mo in moc)
{
if (!(bool)mo[“IPEnabled“])
continue;
Console.WriteLine(“{0} SV
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 187 2017-08-01 23:15 SetIP\SetIP\App.config
文件 7168 2017-08-01 23:17 SetIP\SetIP\bin\Debug\SetIP.exe
文件 187 2017-08-01 23:15 SetIP\SetIP\bin\Debug\SetIP.exe.config
文件 15872 2017-08-01 23:17 SetIP\SetIP\bin\Debug\SetIP.pdb
文件 22984 2017-08-01 23:15 SetIP\SetIP\bin\Debug\SetIP.vshost.exe
文件 187 2017-08-01 23:15 SetIP\SetIP\bin\Debug\SetIP.vshost.exe.config
文件 490 2015-10-22 23:54 SetIP\SetIP\bin\Debug\SetIP.vshost.exe.manifest
文件 6636 2017-08-01 23:16 SetIP\SetIP\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 377 2017-08-01 23:17 SetIP\SetIP\obj\Debug\SetIP.csproj.FileListAbsolute.txt
文件 7168 2017-08-01 23:17 SetIP\SetIP\obj\Debug\SetIP.exe
文件 15872 2017-08-01 23:17 SetIP\SetIP\obj\Debug\SetIP.pdb
文件 0 2017-08-01 23:15 SetIP\SetIP\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2017-08-01 23:15 SetIP\SetIP\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2017-08-01 23:15 SetIP\SetIP\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 4437 2017-08-01 23:16 SetIP\SetIP\Program.cs
文件 1348 2017-08-01 23:15 SetIP\SetIP\Properties\AssemblyInfo.cs
文件 2598 2017-08-01 23:17 SetIP\SetIP\SetIP.csproj
文件 905 2017-08-01 23:15 SetIP\SetIP.sln
..A..H. 16384 2017-08-01 23:16 SetIP\SetIP.v11.suo
目录 0 2017-08-01 23:15 SetIP\SetIP\obj\Debug\TempPE
目录 0 2017-08-01 23:17 SetIP\SetIP\bin\Debug
目录 0 2017-08-01 23:17 SetIP\SetIP\obj\Debug
目录 0 2017-08-01 23:17 SetIP\SetIP\bin
目录 0 2017-08-01 23:17 SetIP\SetIP\obj
目录 0 2017-08-01 23:17 SetIP\SetIP\Properties
目录 0 2017-08-01 23:17 SetIP\SetIP
目录 0 2017-08-01 23:17 SetIP
----------- --------- ---------- ----- ----
102800 27
............此处省略0个文件信息
- 上一篇:C#调用打印机打印
- 下一篇:C#公路曲线中边桩坐标计算
评论
共有 条评论