private void btnAdd_Click(object sender, EventArgs e)
        {
            bool blunique = true;

            //check if a profile already exists with this name
            for (int i = 0; i < Profiles.getProfileCount(); i++)
            {
                if (Profiles.getProfile(i).Name.ToLower() == txtName.Text.ToLower())
                {
                    blunique = false;
                    break;
                }
            }

            if (blunique)
            {

                System.Net.IPAddress ip = new System.Net.IPAddress(new byte[] {0, 0, 0, 0});
                System.Net.IPAddress subnet = new System.Net.IPAddress(new byte[] {0, 0, 0, 0});
                System.Net.IPAddress gateway = new System.Net.IPAddress(new byte[] {0, 0, 0, 0});
                System.Net.IPAddress DNS1;
                System.Net.IPAddress DNS2;
                bool Valid = true;
                Profile newProfile = new Profile();

                //validate all the fields
                if (txtName.Text.Trim() != "")
                {
                    newProfile.Name = txtName.Text;
                }
                else
                {
                    Valid = false;
                }

                if (!rdIPDHCP.Checked)
                {
                    if (System.Net.IPAddress.TryParse(txtIPAddress.Text, out ip))
                    {
                        newProfile.IP = ip.ToString();
                    }
                    else
                    {
                        Valid = false;
                    }

                    if (System.Net.IPAddress.TryParse(txtSubnetMask.Text, out subnet))
                    {
                        newProfile.Subnet = subnet.ToString();
                    }
                    else
                    {
                        Valid = false;
                    }

                    if (System.Net.IPAddress.TryParse(txtDefaultGateway.Text, out gateway))
                    {
                        newProfile.Gateway = gateway.ToString();
                    }
                    else
                    {
                        Valid = false;
                    }
                }

                if (!rdDNSDHCP.Checked)
                {
                    if (System.Net.IPAddress.TryParse(txtDNS1.Text, out DNS1))
                    {
                        newProfile.PrimaryDNS = DNS1.ToString();
                    }

                    if (System.Net.IPAddress.TryParse(txtDNS2.Text, out DNS2))
                    {
                        newProfile.SecondaryDNS = DNS2.ToString();
                    }
                }

                if (Valid)
                {

                    //check the subnet is correct
                    if (checkValidSubnet(subnet))
                    {

                        if(checkInSubnet(ip, gateway, subnet))
                        {

                            Profiles.addProfile(newProfile);
                            Profiles.saveProfiles();

                            m_parent.displayProfiles();
                            this.Dispose();

                        }else{
                            MessageBox.Show("The Gateway is not in the same subnet as the IP Address");
                        }

                    }
                    else
                    {
                        MessageBox.Show("The Subnet you have entered is not a valid subnet");
                    }

                }
                else
                {
                   MessageBox.Show("Profile is not correct. Please ensure all fields are in the correct format");
                }
            }
            else
            {
                MessageBox.Show("A profile by that name already exists.");
            }
        }
Beispiel #2
0
        public void loadProfiles()
        {
            string configFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NetworkPlusPlus");
            configFile = System.IO.Path.Combine(configFile, "config.xml");

            try
            {

                XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
                xmlDoc.Load(configFile);

                XmlNodeList profileslist = xmlDoc.GetElementsByTagName("Profile");

                for (int i = 0; i < profileslist.Count; i++)
                {

                    Profile tmpProfile = new Profile();
                    XmlNodeList nodelist = profileslist[i].ChildNodes;

                    for (int i2 = 0; i2 < nodelist.Count; i2++)
                    {

                        if (nodelist[i2].Name == "Name")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.Name = nodelist[i2].InnerText.ToString();
                            }
                        }

                        if (nodelist[i2].Name == "IPAddress")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.IP = nodelist[i2].InnerText.ToString();
                            }
                        }

                        if (nodelist[i2].Name == "Subnet")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.Subnet = nodelist[i2].InnerText.ToString();
                            }
                        }

                        if (nodelist[i2].Name == "Gateway")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.Gateway = nodelist[i2].InnerText.ToString();
                            }
                        }

                        if (nodelist[i2].Name == "PrimaryDNS")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.PrimaryDNS = nodelist[i2].InnerText.ToString();
                            }
                        }

                        if (nodelist[i2].Name == "SecondaryDNS")
                        {
                            if (nodelist[i2].InnerText != null)
                            {
                                tmpProfile.SecondaryDNS = nodelist[i2].InnerText.ToString();
                            }
                        }

                    }

                    Profiles.addProfile(tmpProfile);

                }
            }
            catch (Exception ex)
            {

                if (ex.Message.ToString().Trim().ToLower() != "root element is missing.") //Exculde problems where the config file is empty
                {
                    if (System.IO.File.Exists(configFile))
                    {
                        string profileBak = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NetworkPlusPlus");
                        profileBak = System.IO.Path.Combine(profileBak, "config" + DateTime.Now.ToString("ddMMyyHHmm") + ".xml.bak");

                        MessageBox.Show("The configuration file was found but is corrupt. Creating new configuration file. All profiles have been lost");
                        System.IO.File.Copy(configFile, profileBak, true);

                    }

                    System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NetworkPlusPlus"));
                    System.IO.FileStream fs = System.IO.File.Create(configFile);
                    fs.Close();

                }

            }
        }
        public void applyProfile(Profile selectedProfile)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index=" + intID);
            ManagementObjectCollection objCol = searcher.Get();

            foreach (ManagementObject mgtObject in objCol)
            {

                if (selectedProfile.IP != "")
                {

                    object[] IPs = { selectedProfile.IP };
                    object[] Subnets = { selectedProfile.Subnet };
                    object[] methodArgs = { IPs, Subnets };

                    object[] GatewayIPs = { selectedProfile.Gateway };
                    object[] Metrics = { "1" };
                    object[] gatewayArgs = { GatewayIPs, Metrics };

                    object returnVal = mgtObject.InvokeMethod("EnableStatic", methodArgs);
                    object returnVal2 = mgtObject.InvokeMethod("SetGateways", gatewayArgs);

                }
                else
                {
                    mgtObject.InvokeMethod("EnableDHCP", null);
                }

                if (selectedProfile.PrimaryDNS == "" && selectedProfile.SecondaryDNS == "")
                {
                    object returnVal3 = mgtObject.InvokeMethod("SetDNSServerSearchOrder", null);
                }
                else
                {
                    object[] DNSServers = { selectedProfile.PrimaryDNS, selectedProfile.SecondaryDNS };
                    object[] DNS = { DNSServers };
                    object returnVal3 = mgtObject.InvokeMethod("SetDNSServerSearchOrder", DNS);
                }

            }
        }
 public static void addProfile(Profile prfl)
 {
     prfs.Add(prfl);
 }