Exemple #1
0
        private void set_dns_Click(object sender, EventArgs e)
        {
            string[] Google_DNS = { "8.8.8.8", "8.8.4.4" };

            WMINetworkHelper wmi_network_helper = new WMINetworkHelper();

            NIC        selected_nic = new NIC();
            List <NIC> nic_names    = wmi_network_helper.GetNICList();

            foreach (NIC nic in nic_names)
            {
                if (nic.NicName.ToLower() == Convert.ToString(nic_nicknames_combobox.SelectedItem).ToLower())
                {
                    selected_nic.NicName = nic.NicName;
                    selected_nic.Guid    = nic.Guid;
                }
            }

            if (wmi_network_helper.SetDNS(selected_nic, Google_DNS))
            {
                MessageBox.Show("New DNS search order added. New DNS should now display.", "Information...", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Error trying to set DNS.", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            nic_nicknames_combobox_SelectedIndexChanged(sender, e);
        }
Exemple #2
0
        /// <summary>
        /// Returns the network card configuration of the specified NIC
        /// </summary>
        /// <param name="nic">Name of the NIC struct</param>
        /// <param name="ipAdresses">Array of IP</param>
        /// <param name="subnets">Array of subnet masks</param>
        /// <param name="gateways">Array of gateways</param>
        /// <param name="dnses">Array of DNS IP</param>
        public NetworkData GetNetworkData(NIC nic)
        {
            NetworkData wmidata = new NetworkData();

            wmidata.IPaddresses = new List <string>();
            wmidata.Subnets     = new List <string>();
            wmidata.Gateways    = new List <string>();
            wmidata.DNSes       = new List <string>();

            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                using (ManagementObjectCollection moc = mc.GetInstances())
                {
                    foreach (ManagementObject mo in moc)
                    {
                        // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                        if ((bool)mo["ipEnabled"])
                        {
                            if (mo["SettingID"].Equals(nic.Guid))
                            {
                                if (mo["IPAddress"] != null)
                                {
                                    wmidata.IPaddresses = ((string[])mo["IPAddress"]).ToList();
                                }

                                if (mo["IPSubnet"] != null)
                                {
                                    wmidata.Subnets = ((string[])mo["IPSubnet"]).ToList();
                                }

                                if (mo["DefaultIPGateway"] != null)
                                {
                                    wmidata.Gateways = ((string[])mo["DefaultIPGateway"]).ToList();
                                }

                                if (mo["DNSServerSearchOrder"] != null)
                                {
                                    wmidata.DNSes = ((string[])mo["DNSServerSearchOrder"]).ToList();
                                }

                                if (mo["DNSDomainSuffixSearchOrder"] != null)
                                {
                                    wmidata.DNSSuffixes = ((string[])mo["DNSDomainSuffixSearchOrder"]).ToList();
                                }

                                if (mo["DHCPEnabled"] != null)
                                {
                                    wmidata.DHCP = ((bool)mo["DHCPEnabled"]);
                                }
                            }
                        }
                    }
                }
            }

            return(wmidata);
        }
Exemple #3
0
        /// <summary>
        /// Set IP for the specified network card name
        /// </summary>
        /// <param name="nic">Name of the NIC struct</param>
        /// <param name="IpAddresses">Comma delimited string containing one or more IP</param>
        /// <param name="SubnetMask">Subnet mask</param>
        /// <param name="Gateway">Gateway IP</param>
        /// <param name="DnsSearchOrder">Comma delimited DNS IP</param>
        public bool SetIP(NIC nic, string IpAddresses, string SubnetMask, string Gateway, string[] DnsSearchOrder)
        {
            bool success = false;

            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                using (ManagementObjectCollection moc = mc.GetInstances())
                {
                    foreach (ManagementObject mo in moc)
                    {
                        // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                        if ((bool)mo["IPEnabled"])
                        {
                            if (mo["SettingID"].Equals(nic.Guid))
                            {
                                try
                                {
                                    using (ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"))
                                    {
                                        newIP["IPAddress"]  = IpAddresses.Split(',');
                                        newIP["SubnetMask"] = new string[] { SubnetMask };

                                        mo.InvokeMethod("EnableStatic", newIP, null);
                                    }

                                    using (ManagementBaseObject newGate = mo.GetMethodParameters("SetGateways"))
                                    {
                                        newGate["DefaultIPGateway"]  = new string[] { Gateway };
                                        newGate["GatewayCostMetric"] = new int[] { 1 };
                                        mo.InvokeMethod("SetGateways", newGate, null);
                                    }

                                    using (ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"))
                                    {
                                        newDNS["DNSServerSearchOrder"] = DnsSearchOrder; //.Split(',');
                                        mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                                    }

                                    success = true;
                                }
                                catch (Exception Ex)
                                {
                                    LogWriter.Exception("Error trying to set IP.", Ex);
                                }
                            }
                        }
                    }
                }
            }

            return(success);
        }
Exemple #4
0
        // This code should be used if not loading DLLs on application load.

        //public WMINetworkHelper()
        //{
        //    try
        //    {
        //        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        //        {
        //            string resourceName = new AssemblyName(args.Name).Name + ".dll";
        //            string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

        //            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
        //            {
        //                Byte[] assemblyData = new Byte[stream.Length];
        //                stream.Read(assemblyData, 0, assemblyData.Length);
        //                return Assembly.Load(assemblyData);
        //            }
        //        };
        //    }
        //    catch (Exception Ex)
        //    {
        //        LogWriter.Exception("Error loading embedded assembly resource. Application is about to crash.", Ex, true);
        //    }
        //}

        /// <summary>
        /// Sets Just the DNS for the specified network card name
        /// This is MY own code and added it since the original code was missing a method to just set the DNS.
        /// </summary>
        /// <param name="nic">Name of the NIC struct</param>
        /// <param name="DnsSearchOrder">Comma delimited DNS IP</param>
        public bool SetDNS(NIC nic, string[] DnsSearchOrder)
        {
            bool success = false;

            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                using (ManagementObjectCollection moc = mc.GetInstances())
                {
                    foreach (ManagementObject mo in moc)
                    {
                        // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                        if ((bool)mo["IPEnabled"])
                        {
                            if (mo["SettingID"].Equals(nic.Guid))
                            {
                                try
                                {
                                    using (ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"))
                                    {
                                        if (newDNS != null)
                                        {
                                            newDNS["DNSServerSearchOrder"] = DnsSearchOrder; //.Split(',');
                                            mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                                            success = true;
                                        }
                                    }
                                }
                                catch (Exception Ex)
                                {
                                    LogWriter.Exception("Error trying to set DNS.", Ex);
                                }
                            }
                        }
                    }
                }
            }

            return(success);
        }
Exemple #5
0
        private void nic_nicknames_combobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            WMINetworkHelper wmi_network_helper = new WMINetworkHelper();
            NetworkData      network_data       = new NetworkData();

            NIC        selected_nic = new NIC();
            List <NIC> nic_names    = wmi_network_helper.GetNICList();

            foreach (NIC nic in nic_names)
            {
                if (nic.NicName.ToLower() == Convert.ToString(nic_nicknames_combobox.SelectedItem).ToLower())
                {
                    selected_nic.NicName = nic.NicName;
                    selected_nic.Guid    = nic.Guid;
                }
            }

            network_data = wmi_network_helper.GetNetworkData(selected_nic);

            if (network_data.IPaddresses.Count > 0)
            {
                current_ipv4_label.Text = network_data.IPaddresses[0];

                if (network_data.IPaddresses.Count > 1)
                {
                    current_ipv6_label.Text = network_data.IPaddresses[1];
                }

                if (network_data.Subnets.Count > 0)
                {
                    current_subnet_label.Text = network_data.Subnets[0];
                }

                if (network_data.Gateways.Count > 0)
                {
                    current_gateway_label.Text = network_data.Gateways[0];
                }

                current_dns_label.Text    = String.Join(" | ", network_data.DNSes.ToArray());
                current_suffix_label.Text = String.Join(" | ", network_data.DNSSuffixes.ToArray());
                current_proxy_label.Text  = IEProxy.ProxyServer;

                if (network_data.DHCP)
                {
                    current_static_ip_label.Text = "No";
                }
                else
                {
                    current_static_ip_label.Text = "Yes";
                }

                if ((System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName).Length > 0)
                {
                    current_workgroup_label.Text = "";
                    current_domain_label.Text    = wmi_network_helper.GetCurrentWorkgroup();
                }
                else
                {
                    current_workgroup_label.Text = wmi_network_helper.GetCurrentWorkgroup();
                    current_domain_label.Text    = "";
                }
            }
        }