首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#或cmd获取本机网上邻居-属性-网络连接 的列表,该如何解决

2012-05-02 
C#或cmd获取本机网上邻居--属性--网络连接 的列表我要这个列表LAN或高速Internet无线网络连接本地连接拨号

C#或cmd获取本机网上邻居--属性--网络连接 的列表
我要这个列表  

LAN或高速Internet

无线网络连接 本地连接

拨号

中国电信 中国移动CMWAP

宽带

宽带连接

我要获取的是 无线网络连接 本地连接 中国电信 中国移动CTWAP 宽带连接 这个列表


我要做一个WIFI转发的软件 wifi方面已经完成了。只有在网络连接设置共享方面 选择 已经连接上的网络。。。

[解决办法]
using System.Net.NetworkInformation;

C# code
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();             foreach (NetworkInterface adapter in nics)            {                bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接                if (Pd1)                {                    Console.WriteLine("网络适配器名称:" + adapter.Name);                    Console.WriteLine("网络适配器标识符:" + adapter.Id);                    Console.WriteLine("适配器连接状态:" + adapter.OperationalStatus.ToString());                                    IPInterfaceProperties ip = adapter.GetIPProperties();     //IP配置信息                    if (ip.UnicastAddresses.Count > 0)                    {                        Console.WriteLine("IP地址:" + ip.UnicastAddresses[0].Address.ToString());                        Console.WriteLine("子网掩码:" + ip.UnicastAddresses[0].IPv4Mask.ToString());                    }                    if (ip.GatewayAddresses.Count > 0)                    {                        Console.WriteLine("默认网关:" + ip.GatewayAddresses[0].Address.ToString());   //默认网关                    }                    int DnsCount = ip.DnsAddresses.Count;                    Console.WriteLine("DNS服务器地址:");   //默认网关                    if (DnsCount > 0)                    {                        //其中第一个为首选DNS,第二个为备用的,余下的为所有DNS为DNS备用,按使用顺序排列                        for (int i = 0; i < DnsCount; i++)                        {                            Console.WriteLine("              " + ip.DnsAddresses[i].ToString());                        }                    }                    Console.WriteLine("网络接口速度:" + (adapter.Speed / 1000000).ToString("0.0") + "Mbps");                    Console.WriteLine("接口描述:" + adapter.Description);                    Console.WriteLine("适配器的媒体访问控制 (MAC) 地址:" + adapter.GetPhysicalAddress().ToString());                    Console.WriteLine("该接口是否只接收数据包:" + adapter.IsReceiveOnly.ToString());                    Console.WriteLine("该接口收到的字节数:" + adapter.GetIPv4Statistics().BytesReceived.ToString());                    Console.WriteLine("该接口发送的字节数:" + adapter.GetIPv4Statistics().BytesSent.ToString());                    Console.WriteLine("该接口丢弃的传入数据包数:" + adapter.GetIPv4Statistics().IncomingPacketsDiscarded.ToString());                    Console.WriteLine("该接口丢弃的传出数据包数:" + adapter.GetIPv4Statistics().OutgoingPacketsDiscarded.ToString());                    Console.WriteLine("该接口有错误的传入数据包数:" + adapter.GetIPv4Statistics().IncomingPacketsWithErrors.ToString());                    Console.WriteLine("该接口有错误的传出数据包数:" + adapter.GetIPv4Statistics().OutgoingPacketsWithErrors.ToString());                    Console.WriteLine("该接口协议未知的数据包数:" + adapter.GetIPv4Statistics().IncomingUnknownProtocolPackets.ToString());                    Console.WriteLine("---------------------------------\n");                }                           }            Console.ReadLine();        }    }}
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace adslname
{
public class Class1
{
#region 获取adsl所有宽带连接名称

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]


public struct RasEntryName //define the struct to receive the entry name
{
public int dwSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256 + 1)]
public string szEntryName;
#if WINVER5
public int dwFlags;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
public string szPhonebookPath;
#endif
}

[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]

public extern static uint RasEnumEntries(
string reserved, // reserved, must be NULL
string lpszPhonebook, // pointer to full path and file name of phone-book file
[In, Out]RasEntryName[] lprasentryname, // buffer to receive phone-book entries
ref int lpcb, // size in bytes of buffer
out int lpcEntries // number of entries written to buffer
);

public static List<string> adslname()
{
List<string> list = new List<string>();
int lpNames = 1;
int entryNameSize = 0;
int lpSize = 0;
RasEntryName[] names = null;
entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
lpSize = lpNames * entryNameSize;
names = new RasEntryName[lpNames];
names[0].dwSize = entryNameSize;
uint retval = RasEnumEntries(null, null, names, ref lpSize, out lpNames);

//if we have more than one connection, we need to do it again
if (lpNames > 1)
{
names = new RasEntryName[lpNames];
for (int i = 0; i < names.Length; i++)
{
names[i].dwSize = entryNameSize;
}
retval = RasEnumEntries(null, null, names, ref lpSize, out lpNames);
}

if (lpNames > 0)
{
for (int i = 0; i < names.Length; i++)
{
list.Add(names[i].szEntryName);
}
}
return list;
}

#endregion
}

}
用adslname()方法就可以获得列表,返回的是一个List<string>数组,每一个元素是一个名称

热点排行