惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - ю意思я

视频播放的进化(一) 图片同步 WPF初体验 C#实现开关显示器功能 图片裁剪 共享文件夹 w32Time服务(NTP)的一些配置 ShutdownAPI 将文件读取到内存中 在windows server 2003服务器上提供NTP时间同步服务 如何与使用DHCP获取IP地址的设备组网 在VS内添加Web Reference 如何消除锯齿,在图片上画出高质量的文字 如何让Firefox3支持迅雷 判断字符串是否为IPv4地址 axWindowsMediaPlayer操作 关于相对路径匹配的问题 ToFriendlyFileSize Class的设计
获取和设置IP地址
ю意思я · 2008-09-02 · via 博客园 - ю意思я

  Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;namespace CommandLine {
    
class Program {
        
static void Main(string[] args) {
            ShowNetwork();
            SetNetwork(
new Networker {
                IpAddress 
= "192.168.15.27",
                Mask 
= "255.255.255.0",
                Gateway 
= "192.168.15.1",
                Dns1 
= "202.96.209.133",
                Dns2 
= "203.94.0.26"
            });

            Console.WriteLine(

"== End ==");
            Console.ReadKey();
        }
public static void ShowNetwork() {
            ManagementObjectSearcher query 
= new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
            ManagementObjectCollection queryCollection 
= query.Get();
            Console.WriteLine(
"==========");
            
foreach (ManagementObject mo in queryCollection) {
                
string description = mo["Description"].ToString();
                
string mac = mo["MACAddress"].ToString();
                
string[] ips = (string[])mo["IPAddress"];
                
string[] masks = (string[])mo["IPSubnet"];
                
string[] gateways = (string[])mo["DefaultIPGateway"];
                
string[] dnses = (string[])mo["DNSServerSearchOrder"];
                Console.WriteLine(
"Description:{0}", description);
                Console.WriteLine(
"MACAddress:{0}", mac);
                
foreach (string each in ips) {
                    Console.WriteLine(
"IPAddress:{0}", each);
                }
                
foreach (string each in masks) {
                    Console.WriteLine(
"Mask:{0}", each);
                }
                
if (gateways != null) {
                    
foreach (string each in gateways) {
                        Console.WriteLine(
"Gateway:{0}", each);
                    }
                }
                
if (dnses != null) {
                    
foreach (string each in dnses) {
                        Console.WriteLine(
"DNSServer:{0}", each);
                    }
                }
                Console.WriteLine(
"==========");
            }
        }
public static void SetNetwork(Networker worker) {
            
string result = "failed";

            ManagementObjectSearcher query 

= new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
            ManagementObjectCollection queryCollection 
= query.Get();
            
string[] ipAddress = new string[] { worker.IpAddress };
            
string[] mask = new string[] { worker.Mask };
            
string[] gateway = null;
            
if (!string.IsNullOrEmpty(worker.Gateway)) {
                gateway 
= new string[] { worker.Gateway };
            }
            
string[] dnses = null;
            List
<string> list = new List<string>();
            
if (!string.IsNullOrEmpty(worker.Dns1)) {
                list.Add(worker.Dns1);
            }
            
if (!string.IsNullOrEmpty(worker.Dns2)) {
                list.Add(worker.Dns2);
            }
            
if (list.Count != 0) {
                dnses 
= list.ToArray();
            }

            ManagementBaseObject inPar 

= null;
            ManagementBaseObject outPar 
= null;
            
foreach (ManagementObject mo in queryCollection) {
                
try {
                    
//设置IP地址和掩码
                    if (ipAddress != null && mask != null) {
                        inPar 
= mo.GetMethodParameters("EnableStatic");
                        inPar[
"IPAddress"= ipAddress;
                        inPar[
"SubnetMask"= mask;
                        outPar 
= mo.InvokeMethod("EnableStatic", inPar, null);
                    }
//设置网关地址
                    if (gateway != null) {
                        inPar 
= mo.GetMethodParameters("SetGateways");
                        inPar[
"DefaultIPGateway"= gateway;
                        outPar 
= mo.InvokeMethod("SetGateways", inPar, null);
                    }
//设置DNS地址
                    if (dnses != null) {
                        inPar 
= mo.GetMethodParameters("SetDNSServerSearchOrder");
                        inPar[
"DNSServerSearchOrder"= dnses;
                        outPar 
= mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
                    }
                    result 
= "succeed";
                } 
catch {
                    result 
= "failed";
                }
                
break;
            }
            Console.WriteLine(
"SetNetwork {0}", result);
        }
    }
}

  Networker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CommandLine {
    
public class Networker {
        
public string IpAddress { getset; }
        
public string MacAddress { getset; }
        
public string Mask { getset; }
        
public string Gateway { getset; }
        
public string Dns1 { getset; }
        
public string Dns2 { getset; }
    }
}

【意思原创,转载请注明出处,谢谢】