Ejemplo n.º 1
0
        public static List<NetworkAdapter> Adapters()
        {
            List<NetworkAdapter> adapters = new List<NetworkAdapter>();

            // capture output of ipconfig /all command
            string ipconfig;
            ConsoleHelper.Run("ipconfig", "/all", out ipconfig);

            string name;
            int start = 0;
            int end = ipconfig.Length;

            do
            {
                name = StringHelper.RegexFindString(ipconfig.Substring(start + 1), "adapter (.+):");
                start = ipconfig.IndexOf(name + ":", start + 1);

                string next = StringHelper.RegexFindString(ipconfig.Substring(start + 1), "adapter (.+):");
                end = ipconfig.IndexOf(next + ":", start + 1);
                if (end < start) end = ipconfig.Length;

                string details = ipconfig.Substring(start, end - start);

                string mediaState = 		StringHelper.RegexFindString(details, "Media State . . . . . . . . . . . : (.+)\n");
                string physicalAddress = 	StringHelper.RegexFindString(details, "Physical Address. . . . . . . . . : (.+)\n");
                string DHCP = 				StringHelper.RegexFindString(details, "DHCP Enabled. . . . . . . . . . . : (.+)\n");
                string ip = 				StringHelper.RegexFindString(details, "IPv4 Address. . . . . . . . . . . : (.+)\n");
                string mask = 				StringHelper.RegexFindString(details, "Subnet Mask . . . . . . . . . . . : (.+)\n");
                string gateway = 			StringHelper.RegexFindString(details, "Default Gateway . . . . . . . . . : (.+)\n");

                if (!string.IsNullOrEmpty(ip) && ip.Contains("("))
                {
                    ip = ip.Substring(0, ip.IndexOf('('));
                }

                if (!string.IsNullOrEmpty(name) && !name.StartsWith("isatap") && !name.StartsWith("Teredo Tunneling"))
                {
                    NetworkAdapter adapter = new NetworkAdapter();
                    adapter.Name = name;
                    adapter.IPAddress = ip;
                    adapter.SubnetMask = mask;
                    adapter.Gateway = gateway;
                    adapter.PhysicalAdddress = physicalAddress;
                    adapter.Connected = (mediaState == "");
                    adapter.DHCP = (DHCP == "Yes");
                    adapters.Add(adapter);
                }

                ConsoleHelper.DebugWriteLine("name:\t\t" + StringHelper.AddQuotes(name));
                ConsoleHelper.DebugWriteLine("mediaState:\t" + StringHelper.AddQuotes(mediaState));
                ConsoleHelper.DebugWriteLine("MAC:\t\t" + StringHelper.AddQuotes(physicalAddress));
                ConsoleHelper.DebugWriteLine("DHCP:\t\t" + StringHelper.AddQuotes(DHCP));
                ConsoleHelper.DebugWriteLine("ip:\t\t" + StringHelper.AddQuotes(ip));	// TODO: remove (Preferred)
                ConsoleHelper.DebugWriteLine("mask:\t\t" + StringHelper.AddQuotes(mask));
                ConsoleHelper.DebugWriteLine("gateway:\t" + StringHelper.AddQuotes(gateway));
                ConsoleHelper.DebugWriteLine(ConsoleHelper.HR);
            } while (!String.IsNullOrEmpty(name));

            return adapters;
        }
Ejemplo n.º 2
0
        public static List <NetworkAdapter> Adapters()
        {
            List <NetworkAdapter> adapters = new List <NetworkAdapter>();

            // capture output of ipconfig /all command
            string ipconfig;

            ConsoleHelper.Run("ipconfig", "/all", out ipconfig);

            string name;
            int    start = 0;
            int    end   = ipconfig.Length;

            do
            {
                name  = StringHelper.RegexFindString(ipconfig.Substring(start + 1), "adapter (.+):");
                start = ipconfig.IndexOf(name + ":", start + 1);

                string next = StringHelper.RegexFindString(ipconfig.Substring(start + 1), "adapter (.+):");
                end = ipconfig.IndexOf(next + ":", start + 1);
                if (end < start)
                {
                    end = ipconfig.Length;
                }


                string details = ipconfig.Substring(start, end - start);

                string mediaState      = StringHelper.RegexFindString(details, "Media State . . . . . . . . . . . : (.+)\n");
                string physicalAddress = StringHelper.RegexFindString(details, "Physical Address. . . . . . . . . : (.+)\n");
                string DHCP            = StringHelper.RegexFindString(details, "DHCP Enabled. . . . . . . . . . . : (.+)\n");
                string ip      = StringHelper.RegexFindString(details, "IPv4 Address. . . . . . . . . . . : (.+)\n");
                string mask    = StringHelper.RegexFindString(details, "Subnet Mask . . . . . . . . . . . : (.+)\n");
                string gateway = StringHelper.RegexFindString(details, "Default Gateway . . . . . . . . . : (.+)\n");

                if (!string.IsNullOrEmpty(ip) && ip.Contains("("))
                {
                    ip = ip.Substring(0, ip.IndexOf('('));
                }

                if (!string.IsNullOrEmpty(name) && !name.StartsWith("isatap") && !name.StartsWith("Teredo Tunneling"))
                {
                    NetworkAdapter adapter = new NetworkAdapter();
                    adapter.Name             = name;
                    adapter.IPAddress        = ip;
                    adapter.SubnetMask       = mask;
                    adapter.Gateway          = gateway;
                    adapter.PhysicalAdddress = physicalAddress;
                    adapter.Connected        = (mediaState == "");
                    adapter.DHCP             = (DHCP == "Yes");
                    adapters.Add(adapter);
                }

                ConsoleHelper.DebugWriteLine("name:\t\t" + StringHelper.AddQuotes(name));
                ConsoleHelper.DebugWriteLine("mediaState:\t" + StringHelper.AddQuotes(mediaState));
                ConsoleHelper.DebugWriteLine("MAC:\t\t" + StringHelper.AddQuotes(physicalAddress));
                ConsoleHelper.DebugWriteLine("DHCP:\t\t" + StringHelper.AddQuotes(DHCP));
                ConsoleHelper.DebugWriteLine("ip:\t\t" + StringHelper.AddQuotes(ip));                   // TODO: remove (Preferred)
                ConsoleHelper.DebugWriteLine("mask:\t\t" + StringHelper.AddQuotes(mask));
                ConsoleHelper.DebugWriteLine("gateway:\t" + StringHelper.AddQuotes(gateway));
                ConsoleHelper.DebugWriteLine(ConsoleHelper.HR);
            } while (!String.IsNullOrEmpty(name));


            return(adapters);
        }