c#实现ip配置的显示

using System;
using Microsoft.Win32;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistryKey start = Registry.LocalMachine;
            RegistryKey cardServiceName, networkKey;
            string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\ CurrentVersion\\NetworkCards";
            string serviceKey = "SYSTEM\\CurrentControlSet\\Services";
            string networkcardKeyName, deviceName;
            string deviceServiceName, serviceName;
            RegistryKey ServiceName = start.OpenSubKey(networkcardKey);
            if (ServiceName == null)
            {
                Console.WriteLine("错误的关键字1");
                return;
            }
            string[] networkCards = ServiceName.GetSubKeyNames();
            ServiceName.Close();
            foreach (string KeyName in networkCards)
            {
                networkcardKeyName = networkcardKey + "\\" + KeyName;
                cardServiceName = start.OpenSubKey(networkcardKeyName);
                if (cardServiceName == null)
                {
                    Console.WriteLine("错误的关键字2:{0}",networkcardKeyName);
                }
                return;
                deviceServiceName=(string)cardServiceName.GetValue("serviceName");
                deviceName = (string)cardServiceName.GetValue("Dedcription");
                Console.WriteLine("\n网卡:{0}", deviceName);
                serviceName = serviceKey + deviceServiceName + "\\Parameters\\Tcpip";
                networkKey = start.OpenSubKey(serviceKey);
                if (networkKey == null)
                {
                    Console.WriteLine("没有Ip配置");
                }
                else
                {
                    string[] ipaddresses = (string[])networkKey.GetValue("IPAddresss");
                    string[] defaultGateways = (string[])networkKey.GetValue("DefaultGateways");
                    string[] subnetmasks = (string[])networkKey.GetValue("SubnetMask");
                    foreach (string ipaddress in ipaddresses)
                    {
                        Console.WriteLine("IP地址:{0}", ipaddress);
                    }
                    foreach (string subnetmask in subnetmasks)
                    {
                        Console.WriteLine("子网掩码:{0}", subnetmask);
                    }
                    foreach (string defaultGateway in defaultGateways)
                    {
                        Console.WriteLine("网关:{0}", defaultGateway);
                    }
                    networkKey.Close();
                }
            }
            start.Close();
        }
    }
}

posted @ 2011-12-13 23:09  finding_job-sunt  阅读(1492)  评论(6编辑  收藏  举报