c# Check NetWork Interfacse工具

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Web;
using System.Windows;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Data.SqlClient;
using System.Net;
using System.Net.NetworkInformation;

namespace CheckChipInformation.V1._00
{
    class Program
    {
        public static List<String> GetAllChipInformaton;
        public static List<String> GetAllChipInformaton_Values;
        public static String GetChipInformation=String.Empty;
        static int Main(string[] args)
        {
            if (args.Length == 2)
            {
                GetAllChipInfo();
                if (args[0] == @"/C")
                {
                    GetChipInformation = args[1];
                    if (CompareString(GetChipInformation))
                    {
                        if (CreateLogFile(GetChipInformation, "MB.DAT"))
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Check Network Interfaces Information Succeed.");
                            Console.ResetColor();
                            return 0;
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;
                }
                else if (args[0] == @"/R")
                {
                    if (CreateNetWorkLog(args[1]))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Read All NetWork Interfaces Information Succeed.");
                        Console.ResetColor();
                        return 0;
                    }
                    else
                        return 1;
                }
                else
                {
                    help();
                    return 1;
                }
            }
            help();
            return 1;
        }


        public static Boolean CreateLogFile(String Str, String FileName)//生成LOG文件
        {
            Boolean Flag = false;
            FileStream fs = new FileStream(FileName,FileMode.Create,FileAccess.Write);
            try
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(Str);
                sw.Close();
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Not Create " + FileName + " Log File!!");
                Console.ResetColor();
                return Flag;
            }
            finally
            {
                fs.Close();
            }
            return Flag;
        }
        public static Boolean CreateNetWorkLog(String FileName)//读取所有网口信息并存档
        {
            Boolean Flag = false;
            FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
            try
            {
                StreamWriter sw = new StreamWriter(fs);
                foreach (String ss in GetAllChipInformaton)
                    sw.WriteLine(ss);
                sw.Close();
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Not Create "+FileName+" Log File!!");
                Console.ResetColor();
                return Flag;
            }
            finally
            {
                fs.Close();
            }
            return Flag;
        }
        public static void help()//帮助信息
        {
            Title();
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(@"/C                   Check Network Interfaces Information.");
            Console.WriteLine(@"/R                   Read All Network Interfaces Information.");
            Console.WriteLine(@"Example_1:*.exe /C 'Realtek PCIe GBE Family'");
            Console.WriteLine(@"Example_2:*.exe /R NetWorkCardInformaton.log");
            Console.ResetColor();
        }
        public static void Title()//标头信息
        {
            Console.Clear();//清屏
            Console.WriteLine("Program Version V1.00 Create Date:2019-04-10");
            Console.WriteLine("Property in Copyright Feres ShenBo.");
            Console.WriteLine("-----------------------------------------------------------");
        }
        public static Boolean CompareString(String Str)//比较字符串
        {
            Boolean Flag = false;
            foreach (String rd in GetAllChipInformaton_Values)
            {
                if (rd.ToUpper().Contains(Str.ToUpper()))
                {
                    Flag = true;
                    /*Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Check Network Interfaces Information Succeed.");
                    Console.ResetColor();*/
                    return Flag;
                }
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Check Network Interfaces Information Fail.");
            Console.ResetColor();
            return Flag;
        }
        public static Boolean GetAllChipInfo()
        {
            Boolean Flag = false;
            try
            {
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                GetAllChipInformaton = new List<String>();
                GetAllChipInformaton_Values = new List<String>();
                int index = 0;
                foreach (NetworkInterface adapter in adapters)
                {
                    index++;
                    GetAllChipInformaton.Add(adapter.Name + "=" + adapter.Description);
                    GetAllChipInformaton_Values.Add(adapter.Description);
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Read NetWork Card InterFace Information Err!!");
                Console.ResetColor();
            }
            return Flag;
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值