資源簡介
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#公路曲線中邊樁坐標計算
評論
共有 條評論