• C#修改本机的IP,网关等网络地址的代码片段


     1  static void SetNetworkAdapter() 
     2  { 
     3 
     4 
     5  ManagementBaseObject inPar = null
     6  ManagementBaseObject outPar = null
     7  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
     8  ManagementObjectCollection moc = mc.GetInstances();  
     9  foreach( ManagementObject mo in moc ) 
    10  {  
    11  if! (bool) mo[ "IPEnabled" ] ) 
    12  continue
    13  
    14  //设置ip地址和子网掩码 
    15  inPar = mo.GetMethodParameters( "EnableStatic" ); 
    16 
    17 
    18  inPar["IPAddress"= new string[] { "10.22.21.111","192.168.10.9" };  
    19  inPar["SubnetMask"= new string[] { "255.255.255.0","255.255.255.0" }; 
    20 
    21 
    22  outPar = mo.InvokeMethod( "EnableStatic", inPar, null ); 
    23  
    24  //设置网关地址 
    25  inPar = mo.GetMethodParameters("SetGateways");  
    26  inPar["DefaultIPGateway"= new string[] { "10.22.21.1","192.168.10.1"};  
    27  outPar = mo.InvokeMethod( "SetGateways", inPar, null ); 
    28  
    29  //设置DNS 
    30  inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); 
    31 
    32 
    33 
    34  inPar["DNSServerSearchOrder"= new string[] {"179.32.42.4","179.32.42.5"}; 
    35 
    36 
    37  outPar = mo.InvokeMethod( "SetDNSServerSearchOrder" ,inPar,null); 
    38  break
    39  } 
    40  } 
    41  
    42  /**//**//**//// <summary> 
    43 
    44 
    45 
    46  /// 显示当前的ip,子网掩码,网关,DNS,网卡地址等信息 
    47  /// </summary> 
    48  static void ShowNetworkAdapterInfo() 
    49  { 
    50 copyright 
    51 
    52 
    53  Console.WriteLine( "****** Current Network Adapter Information ******" ); 
    54  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
    55  ManagementObjectCollection moc = mc.GetInstances(); 
    56  foreach( ManagementObject mo in moc ) 
    57 
    58 
    59  {  
    60  if! (bool) mo[ "IPEnabled" ] ) 
    61  continue
    62  
    63  Console.WriteLine( "{0}\n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"],  
    64  (string) mo["ServiceName"], (string) mo["MACAddress"] ); 
    65  
    66  string[] addresses = (string[]) mo[ "IPAddress" ];  
    67  string[] subnets = (string[]) mo[ "IPSubnet" ]; 
    68  string[] gateways = (string[]) mo["DefaultIPGateway"]; 
    69  string[] dnses = (string[])mo["DNSServerSearchOrder"];  
    70  
    71  Console.WriteLine( " Addresses :" ); 
    72  foreach(string sad in addresses) 
    73  Console.WriteLine( "\t'{0}'", sad ); 
    74  
    75  Console.WriteLine( " Subnets :" ); 
    76 
    77 
    78 
    79  foreach(string sub in subnets ) 
    80  Console.WriteLine( "\t'{0}'", sub ); 
    81  Console.WriteLine(" Gateways:"); 
    82  foreach(string gw in gateways ) 
    83 
    84 
    85 
    86  Console.WriteLine( "\t'{0}'", gw ); 
    87  
    88  Console.WriteLine(" DNS:"); 
    89  foreach(string dns in dnses ) 
    90  Console.WriteLine( "\t'{0}'", dns );  
    91  } 
    92  } 
  • 相关阅读:
    技术总监7年经验——论程序员的职业发展路线
    2.MySQL入门基本操作初体验
    1.MySQL的安装(linux Ubuntu环境下)
    Boot Petalinux Project Using a remote system
    字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
    linux采用模块方法,添加一个新的设备
    在远程服务器上完成本地设备的程序烧写和调试(基于vivado ,SDK软件)
    Linux Master/Baremetal Remote 配置下的裸机调试
    利用Xlinix SDK 建立Linux程序以及对该程序进行调试
    Vivado Launching SDK "Importing Hardware Specification" error的解决方法
  • 原文地址:https://www.cnblogs.com/godwar/p/1743699.html
Copyright © 2020-2023  润新知