private void CleanOutputDir(checkboxItems checkboxItems)
 {
     File.Delete(checkboxItems.filepath);
     string parentDirectory = Directory.GetParent(checkboxItems.filepath).ToString();
     if (File.Exists(parentDirectory + @"\" + Path.GetFileName(checkboxItems.filepath).Replace("events", "customs"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(checkboxItems.filepath).Replace("events", "customs"));
     }
     if (File.Exists(parentDirectory + @"\" + Path.GetFileName(checkboxItems.filepath).Replace("events", "gui"))) {
         File.Delete(parentDirectory + @"\" + Path.GetFileName(checkboxItems.filepath).Replace("events", "gui"));
     }
 }
 private void WriteStepEventFile(string eventsFileDirectory, int stepNumber, checkboxItems checkboxItems)
 {
     StreamWriter writer = new StreamWriter(eventsFileDirectory + @"\step" + stepNumber + "_events.xml");
     try {
         writer.Write(checkboxItems.stepEvents);
     } finally {
         if (writer != null) {
             writer.Flush();
             writer.Close();
             writer.Dispose();
         }
     }
 }
        private void StartSplittingFile(string fileName, string sessionKey, out List<checkboxItems> items)
        {
            if (!Directory.Exists(txtOutputDir.Text)) {
                Directory.CreateDirectory(txtOutputDir.Text);
            }
            string fileRead = string.Empty;

            items = new List<checkboxItems>();
            StreamReader reader = new StreamReader(fileName);
            string eventsFile = "<Steps label=\"Automatically Generated\">";
            try {
                fileRead = reader.ReadToEnd();
                MatchCollection collection = Regex.Matches(fileRead, "<Comment.*EventID=\"(\\d+)\".*>(.*)</Comment>");
                List<string> matches = new List<string>();
                Dictionary<string, string> matchesAndId = new Dictionary<string, string>();
                for (int i = 0; i < collection.Count; i++) {
                    matches.Add(collection[i].Groups[2].ToString());
                    matchesAndId.Add(collection[i].Groups[1].ToString(), collection[i].Groups[2].ToString());
                    //     txtOutput.Text = txtOutput.Text + "\r\n" + collection[i].Groups[1].ToString();
                }

                string newFileRead = Regex.Replace(fileRead, "<Comment.*</Comment>", "</Events></MXClientScript><!-- --><MXClientScript><Events>");
                string[] test = new string[] { "<!-- -->" };
                string[] splitArray = newFileRead.Split(test, StringSplitOptions.RemoveEmptyEntries);

                for (int j = 0; j < splitArray.Length; j++) {
                    if (j < collection.Count) {
                        StreamWriter writer = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml");
                        StreamWriter guiWriter = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                        StreamWriter customsWriter = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_customs.xml");
                        try {
                            guiWriter.Write(" ");
                            customsWriter.Write(@"<Customs></Customs>");
                            writer.Write(splitArray[j]);
                            if (string.IsNullOrEmpty(sessionKey)) {

                                eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" label=\"" + collection[j].Groups[1].ToString() + "\" events=\"step" + (j + 1) + "_events.xml\" gui=\"step" + (j + 1) + "_gui.xml\" customs=\"step" + (j + 1) + "_customs.xml\"></Step>";

                            } else {
                                eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" sessionKey=\"" + sessionKey + "\" label=\"" + collection[j].Groups[1].ToString() + "\" events=\"step" + (j + 1) + "_events.xml\" gui=\"step" + (j + 1) + "_gui.xml\" customs=\"step" + (j + 1) + "_customs.xml\"></Step>";

                            }
                            checkboxItems item = new checkboxItems();
                            item.filepath = txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml";
                            item.stepName = "step" + (j + 1) + "_events.xml";
                            item.stepTitle = collection[j].Groups[2].ToString();
                            if (j == 0) {
                                item.startStepId = 0;
                            } else {
                                item.startStepId = items[j-1].endStepId+1;
                            }
                            item.endStepId = Convert.ToInt32(collection[j].Groups[1].Value);
                            item.stepEvents = splitArray[j];
                            items.Add(item);
                        } finally {
                            if (writer != null) {
                                writer.Close();
                            }
                            if (guiWriter != null) {
                                guiWriter.Close();
                            }
                            if (customsWriter != null) {
                                customsWriter.Close();
                            }
                        }
                    } else {
                        StreamWriter writer = new StreamWriter(txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml");
                        try {

                            writer.Write(splitArray[j]);

                            if (string.IsNullOrEmpty(sessionKey)) {

                                eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" label=\"Exit\" events=\"step" + (j + 1) + "_events.xml\"></Step>";

                            } else {
                                eventsFile = eventsFile + "\r\n <Step id=\"" + (j + 1) + "\" sessionKey=\"" + sessionKey + "\" label=\"Exit\" events=\"step" + (j + 1) + "_events.xml\"></Step>";

                            }

                            //remove customs and gui for exit step
                            File.Delete(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                            File.Delete(txtOutputDir.Text + @"\step" + (j + 1) + "_gui.xml");
                            checkboxItems item = new checkboxItems();
                            item.filepath = txtOutputDir.Text + @"\step" + (j + 1) + "_events.xml";
                            item.stepName = "step" + (j + 1) + "_events.xml";
                            item.startStepId = items[j - 1].endStepId + 1;
                            item.endStepId= 999;
                            item.stepTitle = "Exit";
                            item.stepEvents = splitArray[j];
                            items.Add(item);
                            // chkLstAllStepEvents.Items.Add(item);
                        } finally {
                            if (writer != null) {
                                writer.Close();
                            }
                        }

                    }

                }
            } finally {
                if (reader != null) {
                    reader.Close();
                }
            }

            StreamWriter eventsfilesWriter = new StreamWriter(txtOutputDir.Text + @"\eventsfiles.xml");
            try {
                eventsFile = eventsFile + "</Steps>";
                eventsfilesWriter.Write(eventsFile);
                oldEventsFile = eventsFile;
            } finally {
                if (eventsfilesWriter != null) {
                    eventsfilesWriter.Close();
                }
            }
        }