学习C#下注册表编程:获取网卡信息(未控制异常)

随手写的一段,win7和xp sp2下测试可用,如果ip设置自动获取会报错。

using System;
using Microsoft.Win32;

namespace GetNetworkCardInfo
{
    class Program
    {
        static void Main(string[] args)
        {

            using (RegistryKey
                Root_Key = Registry.LocalMachine,
                NetworkCards_Key = Root_Key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards"))
            {
                foreach (string Key in NetworkCards_Key.GetSubKeyNames())
                {
                    string Description = (string)Root_Key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" + Key).GetValue("Description");
                    string ServiceName = (string)Root_Key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" + Key).GetValue("ServiceName");
                    Console.WriteLine("网卡:{0}",Description);
                    RegistryKey Interfaces_Key = Root_Key.OpenSubKey(@"SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\" + ServiceName);
                    Console.WriteLine("IP: {0}\n子网掩码:{1}\n网关:{2}\n", ((string[])Interfaces_Key.GetValue("IPAddress"))[0], ((string[])Interfaces_Key.GetValue("SubnetMask"))[0], ((string[])Interfaces_Key.GetValue("DefaultGateway"))[0]);
                    Console.ReadKey();
                }
            }
        }
    }
}

转载于:https://www.cnblogs.com/wangyu1204/archive/2011/12/14/2288012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值