如何在获取本地的IP信息

monking007 2005-02-04 02:26:18
请问大家怎么获取本地的IP信息
就像ipconfig /all 获得的IP信息
...全文
444 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggdayup 2005-02-17
  • 打赏
  • 举报
回复
2.Using WMI
Win32 NetworkAdapterConfiguration Data Fields:

DefaultIPGateway
An array of IP router addresses assigned to the device

Description
The description of the network device

DHCPEnabled
Whether the device dynamically assigns its IP address

DHCPServer
The DHCP server used to assign an IP address

DNSHostName
The DNS host that is used for resolving hostnames

IPAddress
An array of IP addresses assigned to the device

IPEnabled
Whether the device uses IP on the network

IPSubnet
An array of IP subnet addresses used on the device

MACAddress
The Ethernet MAC address assigned to the network device
--------------------------------------------------------------------
1 Create a ManagementObjectSearcher object that contains a SQL Select statement for the

database table.
创建包含SQL查询语句的ManagementObjectSearcher object。

2 Create a ManagementObjectCollection object to obtain the result set from executing the SQL

query.
创建ManagementObjectCollection object用来获得SQL执行后返回的结果。

3 In a foreach loop, assign a new ManagementObject for each object in the Management-

ObjectCollection.
创建foreach循环,为每个ManagementObjectCollection中的ManagementObject赋值。

4 Assign the desired data fields to regular type variables.
-------------------------------------------
WMICardGrab.cs program

using System;
using System.Management;
class WMICardGrab
{
public static void Main ()
{
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Â
Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
string[] defaultgateways =
(string[])mo["DefaultIPGateway"];
Console.WriteLine("Network Card: {0}",
mo["Description"]);
Console.WriteLine(" MAC Address: {0}",
mo["MACAddress"]);
foreach(string ipaddress in addresses)
{
Console.WriteLine(" IP Address: {0}",
ipaddress);
}
foreach(string subnet in subnets)
{
Console.WriteLine(" Subnet Mask: {0}", subnet);
}
foreach(string defaultgateway in defaultgateways)
{
Console.WriteLine(" Gateway: {0}",
defaultgateway);
}
}
}
}
-------------------------------------------
输出结果:
C:\>WMICardGrab
Network Card: D-Link DE660 PCMCIA LAN adapter
MAC Address: 00:80:C8:BC:CE:C3
IP Address: 192.168.1.6
Subnet Mask: 255.255.255.0
Gateway: 192.168.1.1
C:\>
--------------------------------------------
Using WMi可以获得更多的内容!


ggdayup 2005-02-17
  • 打赏
  • 举报
回复
摘录自 “Sybase:C# Network Programming"

补充两种办法:1 Using Registry. 2 Using WMI
1 Using Registry
Microsoft.Win32.Registry--包含注册表的基础键值名称:Current_User, Local_Machine, Classes_Root, Current_Config, Dyn_Data, and Users.你必须使用其中之一。
Microsoft.Win32.RegistryKey--包含操作(查询,修改注册表键值或者数据)的Class和Method。

CardGrab.cs program
-------------------------------
using System;
using Microsoft.Win32;
class CardGrab
{
public static void Main ()
{
RegistryKey start = Registry.LocalMachine;
//你必须定义一个Root Key。

RegistryKey cardServiceName, networkKey;
string networkcardKey = "SOFTWARE\\Microsoft\\ ?
Windows NT\\CurrentVersion\\NetworkCards";
string serviceKey =
"SYSTEM\\CurrentControlSet\\Services\\";
//打开子键。

string networkcardKeyName, deviceName;
string deviceServiceName, serviceName;
RegistryKey serviceNames =
start.OpenSubKey(networkcardKey);
if (serviceNames == null)
{
Console.WriteLine("Bad registry key");
return;
}
string[] networkCards = serviceNames.GetSubKeyNames();
//GetSubKeyNames() 方法

serviceNames.Close();
foreach(string keyName in networkCards)
{
networkcardKeyName = networkcardKey + "\\" + keyName;
cardServiceName = start.OpenSubKey(networkcardKeyName);
if (cardServiceName == null)
{
Console.WriteLine("Bad registry key: {0}",
networkcardKeyName);
return;
}
deviceServiceName =
(string)cardServiceName.GetValue("ServiceName");
deviceName =
(string)cardServiceName.GetValue("Description");
Console.WriteLine("\nNetwork card: {0}", deviceName);

serviceName = serviceKey + deviceServiceName +
"\\Parameters\\Tcpip";
networkKey = start.OpenSubKey(serviceName);
if (networkKey == null)
{
Console.WriteLine(" No IP configuration set");
} else
{
string[] ipaddresses =
(string[])networkKey.GetValue("IPAddress");
string[] defaultGateways =
(string[])networkKey.GetValue("DefaultGateway");
string[] subnetmasks =
(string[])networkKey.GetValue("SubnetMask");
foreach(string ipaddress in ipaddresses)
{
Console.WriteLine(" IP Address: {0}",ipaddress);
}
foreach(string subnetmask in subnetmasks)
{
Console.WriteLine(" Subnet Mask: {0}",
subnetmask);
}
foreach(string defaultGateway in defaultGateways)
{
Console.WriteLine(" Gateway: {0}",
defaultGateway);
}
networkKey.Close();
}
}
start.Close();
}
}
-------------------------------------
输出结果:
C:\>CardGrab
Network card: D-Link DE660 PCMCIA LAN adapter
IP Address: 192.168.1.6
Subnet Mask: 255.255.255.0
Gateway: 192.168.1.1
C:\>
--------------------------------------
sssa2000 2005-02-17
  • 打赏
  • 举报
回复
直接调用dos命令 ipconfig>1.txt
这个最简单了
monking007 2005-02-17
  • 打赏
  • 举报
回复
monking007 2005-02-04
  • 打赏
  • 举报
回复
关注
egxsun 2005-02-04
  • 打赏
  • 举报
回复
up
zjf_dl 2005-02-04
  • 打赏
  • 举报
回复
up
monking007 2005-02-04
  • 打赏
  • 举报
回复
有什么方法可以直接获得本机的
IP,Mac,Gateway,DNS等等信息呢?
liduke 2005-02-04
  • 打赏
  • 举报
回复
如果要想获得地址,需要用sendarp这个函数来实现。具体的代码如下:
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);

Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的ip

try
{
Byte[] macinfo=new Byte[6];
Int32 length=6;

IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);

Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);


}
catch(Exception err)
{
Console.WriteLine(err);
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧