全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1264
推到 Plurk!
推到 Facebook!

How to obtain the current TCP-IP configuation for

 
conundrum
尊榮會員


發表:893
回覆:1272
積分:643
註冊:2004-01-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-09-18 21:35:01 IP:61.64.xxx.xxx 未訂閱
How to obtain the current TCP-IP configuation for all adapters How to obtain the current TCP/IP configuation for all adapters By Gary Caldwell An article on obtaining the current TCP/IP configuration for all .NET supported OSs. http://www.codeproject.com/dotnet/netconfig.asp • Download demo project - 6.42 Kb • Download source - 32.2 Kb Introduction Obtaining the current TCP/IP configuration, is not provided by the standard .NET class libraries. In order to work on all the standard desktop OSs supported by .NET, at least two different approaches must be taken. For more recent OSs such as Windows XP, WMI contains objects that can be queried to obtain this information. The System.Management namespace provides access to WMI objects in managed code. For older OSs like Windows 98, an entirely different approach is needed. Since running the .NET Framework on Windows 98 also requires Internet Explorer 5.x or higher we can also assume that the library IPHLPAPI.DLL is available. This library commonly called IPHelper provides the API calls needed to obtain network configuration. Since IPHLPAPI is not managed code, a managed code wrapper needs to be created to use the .NET Interop services to invoke IPHLPAPI routines. Using the code The source code project provides two classes. The primary class is called NetworkConfiguration. NetworkConfiguration makes the determination internally regarding whether to use WMI or IPHLPAPI.DLL. It provides public methods such as GetTCPIPSettings() that returns an ArrayList of AdapterTCPIPSettings instances. AdapterSettings contains the basic configuration data such as the adapter description, IP addresses, subnets, and gateways. There are several other methods in NetworkConfiguration that serve as helper functions to obtain very task specific information from GetTCPIPSettings(). GetDNSServers() is one such method. The second class is the managed code wrapper that calls into several APIs from IPHLPAPI.DLL. It is used internally by NetworkConfiguration only for OSs that do not support WMI. This class can also be used outside of NetworkConfiguration, if needed. An example of using NetworkConfiguration is provided in the downloadable example attached to this article. The following code snippet demonstrates enumerating through the adapters and doing something with its members. using MooseNet; ... ArrayList settings = NetworkConfiguration.GetTCPIPSettings(); foreach ( NetworkConfiguration.AdapterTCPIPSettings setting in settings ) { if ( setting._description != null ) { ... } ... } Points of interest Something to keep in mind is that the WMI query can be rather slow. Do not call GetTCPIPSettings() very often in your code. The helper methods in NetworkConfiguration use a caching mechanism to avoid this. Another thing is that the AdapterSettings instances will frequently contain null references for data that can not be obtained. This should be checked before using any of its values. You will also notice that these classes are part of a namespace called MooseNet. MooseNet is my open source networking library written in C#. Please check out my project page at http://www.hypermoose.com. You will find many useful classes for handling protocols such as UPnP, SMTP, POP3 and DNS there. History • Initial version - August 10, 2003 Gary Caldwell Click here to view Gary Caldwell's online profile. Other popular .NET articles: • Expresso - A Tool for Building and Testing Regular Expressions For learning, building, and debugging .NET Framework regular expressions • Getting to know IExtenderProvider A walkthrough in the creation of a trivial IExtenderProvider component • WIA Scripting and .NET How to use Windows Image Acquisition on Windows XP. Useful for integrating scanners, digital cameras, webcams and still-video. • Delta Forth .NET World's first Forth compiler for the .NET platform
系統時間:2024-05-17 17:56:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!