private void NextButton_Click(object sender, EventArgs e)
        {
            if (state == 0)
            {
                /*StreamReader sr = new StreamReader(path);
                 * string line = sr.ReadLine();
                 * string[] parsed = line.Split(new String[] { " = " }, StringSplitOptions.None);
                 * while (line != null)
                 * {
                 *  parsed = line.Split(new String[] { " = " },StringSplitOptions.None);
                 *  if (parsed.Length > 1) {
                 *      Policies.Add(parsed[0], Int32.Parse(parsed[parsed.Length - 1]));
                 *  }
                 *
                 *  /*parsed = line.Split(new Char[] { ' ', '=', ' ' });
                 *  if (parsed.Length > 1)
                 *  {
                 *      if (Policies.ContainsKey(parsed[0]))
                 *      {
                 *          advice.Add(Policies[parsed[0]].DynamicInvoke(Int32.Parse(parsed[parsed.Length-1])) + "\n");
                 *      }
                 *  }*/
                //        line = sr.ReadLine();
                //    }
                //label1.Text = advice;


                var guid   = new Guid("31B2F340-016D-11D2-945F-00C04FB984F9");
                var domain = new GPDomain(System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName);
                gpo = domain.GetGpo(guid);
                var gpoReport = gpo.GenerateReport(ReportType.Xml);

                //gpb = gpo.Backup(Path.GetTempPath(), null);
                //path = Path.GetTempPath() + "{" + gpb.Id + "}\\DomainSysvol\\GPO\\Machine\\microsoft\\windows nt\\SecEdit\\GptTmpl.inf";
                //StreamReader sr = new StreamReader(path);

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(gpoReport);

                List <XmlNode> extensionData = new List <XmlNode>();
                XmlNode        extIter       = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData");
                while (extIter != null)
                {
                    if (extIter.Name == "ExtensionData")
                    {
                        extensionData.Add(extIter);
                    }
                    extIter = extIter.NextSibling;
                }

                extIter = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData");
                while (extIter != null)
                {
                    if (extIter.Name == "ExtensionData")
                    {
                        extensionData.Add(extIter);
                    }
                    extIter = extIter.NextSibling;
                }

                /*XmlNode extensiondata1 = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData");
                 * MessageBox.Show(extensiondata1.ToString());
                 * XmlNode extensiondata5 = extensiondata1.NextSibling.NextSibling.NextSibling.NextSibling;
                 * MessageBox.Show(extensiondata5.ToString());
                 * XmlNode extensiondata6 = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData");
                 * MessageBox.Show(extensiondata6.ToString());
                 * MessageBox.Show(extensions.ToString());*/
                var i = 2;
                foreach (XmlNode data in extensionData)
                {
                    XmlNode extensions = data.FirstChild;
                    if (data == extensionData[0])
                    {
                        foreach (XmlNode pol in extensions)
                        {
                            if (pol.Name == "q1:Account")
                            {
                                var name = GetChildByName(pol, "q1:Name");
                                var num  = GetChildByName(pol, "q1:SettingNumber");
                                var tf   = GetChildByName(pol, "q1:SettingBoolean");
                                if (name != null && num != null)
                                {
                                    Policies.Add(pol.ChildNodes[0].InnerText, Int32.Parse(pol.ChildNodes[1].InnerText));
                                }
                                if (name != null && tf != null)
                                {
                                    if (tf.InnerText == "true")
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 1);
                                    }
                                    else
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 0);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (XmlNode pol in extensions)
                        {
                            if (pol.Name == "q" + i.ToString() + ":Policy")
                            {
                                var name  = GetChildByName(pol, "q" + i.ToString() + ":Name");
                                var state = GetChildByName(pol, "q" + i.ToString() + ":State");
                                if (name != null && state != null)
                                {
                                    if (state.InnerText == "Enabled")
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 1);
                                    }
                                    else if (state.InnerText == "Disabled")
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 0);
                                    }
                                    else
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 2);
                                    }
                                }
                            }
                        }
                        i++;
                    }
                }



                XmlDocument policies = new XmlDocument();
                //XmlDocument updater = new XmlDocument();
                Assembly assembly = Assembly.GetExecutingAssembly();
                var      a        = assembly.GetManifestResourceNames();
                Stream   stream   = assembly.GetManifestResourceStream("WindowsFormsApp1.PoliciesXML.xml");
                //Stream stream1 = assembly.GetManifestResourceStream("WindowsFormsApp1.UpdaterXML.xml");
                policies.Load(stream);
                //updater.Load(stream1);

                XmlNode p = policies.ChildNodes[1].ChildNodes[0];
                //XmlNode u = updater.ChildNodes[1].ChildNodes[0];

                while (p != null)
                {
                    if (p.NodeType.ToString() == "Element")
                    {
                        Pols.Add(new Policy(p));
                    }
                    p = p.NextSibling;
                    //u = u.NextSibling;
                }


                maxPage = Pols.Count;
                state   = 1;
                GuidanceButton.Visible = true;
                // UpdateButton.Visible = true;
                MarkAsDone.Visible = true;
                NextButton.Text    = "Next >>";
            }
            if (state == 1)
            {
                if (page == maxPage)
                {
                    InfoBox.Text           = "";
                    state                  = 2;
                    PrevButton.Visible     = false;
                    GuidanceButton.Visible = false;
                    //UpdateButton.Visible = false;
                }
                else
                {
                    var name = Pols[page].getName();
                    if (Policies.ContainsKey(name))
                    {
                        InfoBox.Text = Pols[page].check(Policies[name]).Trim();
                    }
                    else
                    {
                        InfoBox.Text = Pols[page].useDefault().Trim();
                    }
                    if (Pols[page].guidance() == "")
                    {
                        GuidanceButton.Visible = false;
                    }
                    else
                    {
                        GuidanceButton.Visible = true;
                    }

                    page += 1;
                    if (page == maxPage)
                    {
                        NextButton.Text = "Finish";
                    }
                    if (page == 2)
                    {
                        PrevButton.Visible = true;
                    }
                }
            }
            else
            {
                this.Close();
            }
        }
        private void NextButton_Click(object sender, EventArgs e)
        {
            if (state == 0)
            {
                //Gets GP XML

                var guid   = new Guid("31B2F340-016D-11D2-945F-00C04FB984F9");
                var domain = new GPDomain(System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName);
                gpo = domain.GetGpo(guid);
                var         gpoReport = gpo.GenerateReport(ReportType.Xml);
                XmlDocument doc       = new XmlDocument();
                doc.LoadXml(gpoReport);

                //Parses GP XML

                List <XmlNode> extensionData = new List <XmlNode>();
                XmlNode        extIter       = GetChildByName(GetChildByName(doc.LastChild, "Computer"), "ExtensionData");
                while (extIter != null)
                {
                    if (extIter.Name == "ExtensionData")
                    {
                        extensionData.Add(extIter);
                    }
                    extIter = extIter.NextSibling;
                }

                extIter = GetChildByName(GetChildByName(doc.LastChild, "User"), "ExtensionData");
                while (extIter != null)
                {
                    if (extIter.Name == "ExtensionData")
                    {
                        extensionData.Add(extIter);
                    }
                    extIter = extIter.NextSibling;
                }

                var i = 2;
                foreach (XmlNode data in extensionData)
                {
                    XmlNode extensions = data.FirstChild;
                    if (data == extensionData[0])
                    {
                        foreach (XmlNode pol in extensions)
                        {
                            if (pol.Name == "q1:Account")
                            {
                                var name = GetChildByName(pol, "q1:Name");
                                var num  = GetChildByName(pol, "q1:SettingNumber");
                                var tf   = GetChildByName(pol, "q1:SettingBoolean");
                                if (name != null && num != null)
                                {
                                    Policies.Add(pol.ChildNodes[0].InnerText, Int32.Parse(pol.ChildNodes[1].InnerText));
                                }
                                if (name != null && tf != null)
                                {
                                    if (tf.InnerText == "true")
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 1);
                                    }
                                    else
                                    {
                                        Policies.Add(pol.ChildNodes[0].InnerText, 0);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (GetChildByName(data, "Name") != null && GetChildByName(data, "Name").InnerText == "Windows Firewall")
                        {
                            //Special case as the firewall is displayed differently

                            foreach (XmlNode pol in extensions)
                            {
                                if (pol.Name == "q" + i + ":DomainProfile")
                                {
                                    var lpm      = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge");
                                    var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Domain Profile - Apply local firewall rules", lpmState);
                                    var efw      = GetChildByName(pol, "q" + i + ":EnableFirewall");
                                    var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Domain Profile", efwState);
                                    //var ibc = GetChildByName(pol, "q5:DefaultInboundAction");
                                    //var ibcState = (ibc == null) ? 0 : (ibc.InnerText=="true")?1:0;
                                    //var obc = GetChildByName(pol, "q5:DefaultOutboundAction");
                                    //var obcState = (obc == null) ? 0 : (obc.InnerText == "true") ? 1 : 0;
                                }
                                else if (pol.Name == "q" + i + ":PrivateProfile")
                                {
                                    var lpm      = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge");
                                    var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Private Profile - Apply local firewall rules", lpmState);
                                    var efw      = GetChildByName(pol, "q" + i + ":EnableFirewall");
                                    var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Private Profile", efwState);
                                }
                                else if (pol.Name == "q5:PublicProfile")
                                {
                                    var lpm      = GetChildByName(pol, "q" + i + ":AllowLocalPolicyMerge");
                                    var lpmState = (lpm == null) ? 2 : (lpm.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Public Profile - Apply local firewall rules", lpmState);
                                    var efw      = GetChildByName(pol, "q" + i + ":EnableFirewall");
                                    var efwState = (efw == null) ? 2 : (efw.InnerText == "true") ? 1 : 0;
                                    Policies.Add("Public Profile", efwState);
                                }
                            }
                        }
                        else
                        {
                            foreach (XmlNode pol in extensions)
                            {
                                if (pol.Name == "q" + i.ToString() + ":Policy")
                                {
                                    //Parses enabled/disabled/not configured to numbers

                                    var name  = GetChildByName(pol, "q" + i.ToString() + ":Name");
                                    var state = GetChildByName(pol, "q" + i.ToString() + ":State");
                                    if (name != null && state != null)
                                    {
                                        if (state.InnerText == "Enabled")
                                        {
                                            Policies.Add(pol.ChildNodes[0].InnerText, 1);
                                        }
                                        else if (state.InnerText == "Disabled")
                                        {
                                            Policies.Add(pol.ChildNodes[0].InnerText, 0);
                                        }
                                        else
                                        {
                                            Policies.Add(pol.ChildNodes[0].InnerText, 2);
                                        }
                                    }
                                }
                            }
                        }
                        i++;
                    }
                }

                //Parses PoliciesXML to the dictionary to generate the pages

                XmlDocument policies = new XmlDocument();
                Assembly    assembly = Assembly.GetExecutingAssembly();
                var         a        = assembly.GetManifestResourceNames();
                Stream      stream   = assembly.GetManifestResourceStream("WindowsFormsApp1.PoliciesXML.xml");
                policies.Load(stream);

                XmlNode p = policies.ChildNodes[1].ChildNodes[0];

                while (p != null)
                {
                    if (p.NodeType.ToString() == "Element")
                    {
                        Pols.Add(new PolicySection(p));
                    }
                    p = p.NextSibling;
                }

                //Configure first page

                state = 1;
                GuidanceButton.Visible = true;
                MarkAsDone.Visible     = true;
                NextButton.Text        = "Next >>";
                this.Text = Pols[section].name;
            }
            //If within pages
            if (state == 1)
            {
                if (!Pols[section].isPolicyAt(page - 1) && page != 0 && section == Pols.Count - 1)
                {
                    //Exit to finish page

                    InfoBox.Text           = "";
                    state                  = 2;
                    PrevButton.Visible     = false;
                    GuidanceButton.Visible = false;
                    MarkAsDone.Visible     = false;
                    NameLabel.Text         = "";
                    NextButton.Text        = "Finish";
                }
                else
                {
                    if (!Pols[section].isPolicyAt(page - 1) && page != 0)
                    {
                        //Change section

                        section  += 1;
                        this.Text = Pols[section].name;
                        page      = 0;
                    }
                    if (page == 0)
                    {
                        //Display header page for page 0

                        NameLabel.Text = "";
                        InfoBox.Text   = Pols[section].headerText(Policies);
                        if (Pols[section].isSafe())
                        {
                            SkipButton.Visible = true;
                        }
                        else
                        {
                            SkipButton.Visible = false;
                        }
                        GuidanceButton.Visible = false;
                        page += 1;
                        MarkAsDone.Visible = false;
                    }
                    else
                    {
                        //Display individual policy page

                        SkipButton.Visible = false;
                        var currentPolicy = Pols[section].policyAt(page - 1);
                        var name          = currentPolicy.getName();
                        NameLabel.Text = name;
                        InfoBox.Height = 329 - NameLabel.Height;             //Avoids height overflow on name
                        InfoBox.Top    = 18 + NameLabel.Height;
                        if (Policies.ContainsKey(name))
                        {
                            //Use setting found
                            InfoBox.Text        = currentPolicy.check(Policies[name]).Trim();
                            NameLabel.ForeColor = (currentPolicy.isRecommended(Policies[name])) ? currentPolicy.recColour() : currentPolicy.nonRecColour();
                        }
                        else
                        {
                            //Otherwise use default
                            InfoBox.Text        = currentPolicy.useDefault().Trim();
                            NameLabel.ForeColor = (currentPolicy.isRecommended(-1)) ? currentPolicy.recColour() : currentPolicy.nonRecColour();
                        }
                        if (currentPolicy.guidance() == "")
                        {
                            GuidanceButton.Visible = false;
                        }
                        else
                        {
                            GuidanceButton.Visible = true;
                        }

                        page += 1;
                        PrevButton.Visible = true;
                        MarkAsDone.Visible = true;
                        //Very last page show finish instead of next
                        if (!Pols[section].isPolicyAt(page - 1) && section == Pols.Count - 1)
                        {
                            NextButton.Text    = "Finish";
                            MarkAsDone.Visible = false;
                        }
                    }
                }
            }
            else
            {
                this.Close();
            }
        }