private void fillFilesMap() { if (File.Exists("TextFiles.xml")) { XmlDocument doc = new XmlDocument(); doc.Load("TextFiles.xml"); XmlNodeList files = doc.GetElementsByTagName("FileInfo"); foreach (XmlNode n in files) { XmlNodeList attributes = n.ChildNodes; FILE f = new FILE(); f.fileName = attributes[0].InnerText; f.path = attributes[1].InnerText; //MessageBox.Show(attributes[2].InnerText); string[] cats = attributes[2].InnerText.Split(' '); foreach (string c in cats) { if (c.Length != 0) { f.fileCategories.Add(c); } //MessageBox.Show(c); } mapOfFiles[f.fileName] = f; //MessageBox.Show(f.fileName + ' ' + mapOfFiles[f.fileName].fileCategories.Count.ToString()); } } }
private void newFileButton_Click(object sender, EventArgs e) { string str = textBox1.Text; str = str + ".txt"; string Url; FolderBrowserDialog d = new FolderBrowserDialog(); DialogResult res = d.ShowDialog(); if (res == DialogResult.OK) { Url = d.SelectedPath + "\\" + str; } else { //string path = @"C:\Users\Menna\Source\Repos\File-Search-Engine-System\Files\"; Url = path + str + ".txt"; } bool fg = false; for (int i = 0; i < catCheckList.Items.Count; i++) { if (catCheckList.GetItemCheckState(i) == CheckState.Checked) { fg = true; break; } } if (str == ".txt") { MessageBox.Show("Please Enter A Valid Name."); } else if (fg == false) { MessageBox.Show("Please Select At Least 1 Category For The File."); } else { if (!File.Exists(Url)) { File.Create(Url).Dispose(); MessageBox.Show("File Added Successfully. Click On Edit To Start Writing In The File."); if (!File.Exists("TextFiles.xml")) { List <string> categories = new List <string>(); XmlWriter writer = XmlWriter.Create("TextFiles.xml"); writer.WriteStartDocument(); writer.WriteStartElement("Table"); writer.WriteAttributeString("Name", "Files"); writer.WriteStartElement("FileInfo"); writer.WriteStartElement("FileName"); writer.WriteString(textBox1.Text); writer.WriteEndElement(); writer.WriteStartElement("Path"); writer.WriteString(Url); writer.WriteEndElement(); writer.WriteStartElement("Categories"); for (int i = 0; i < catCheckList.Items.Count; i++) { if (catCheckList.GetItemCheckState(i) == CheckState.Checked) { writer.WriteString(Categories_Name[i]); writer.WriteString(" "); categories.Add(Categories_Name[i]); } } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Dispose(); FILE f = new FILE(); f.fileName = textBox1.Text; f.path = Url; Home.mapOfFiles[f.fileName] = f; foreach (string cat in categories) { Home.mapOfFiles[f.fileName].fileCategories.Add(cat); } fileCombo.Items.Add(f.fileName); } else { List <string> categories = new List <string>(); XmlDocument doc = new XmlDocument(); XmlElement FileInfo = doc.CreateElement("FileInfo"); XmlElement node = doc.CreateElement("FileName"); node.InnerText = textBox1.Text; FileInfo.AppendChild(node); node = doc.CreateElement("Path"); node.InnerText = Url; FileInfo.AppendChild(node); node = doc.CreateElement("Categories"); bool f = false; string node_text = ""; for (int i = 0; i < catCheckList.Items.Count; i++) { if (catCheckList.GetItemCheckState(i) == CheckState.Checked) { if (f == false) { node_text = Categories_Name[i]; node_text += " "; f = true; } else { node_text += Categories_Name[i]; node_text += " "; } categories.Add(Categories_Name[i]); } } node.InnerText = node_text; FileInfo.AppendChild(node); doc.Load("TextFiles.xml"); XmlElement root = doc.DocumentElement; root.AppendChild(FileInfo); doc.Save("TextFiles.xml"); FILE ff = new FILE(); ff.fileName = textBox1.Text; ff.path = Url; Home.mapOfFiles[ff.fileName] = ff; foreach (string cat in categories) { Home.mapOfFiles[ff.fileName].fileCategories.Add(cat); } fileCombo.Items.Add(ff.fileName); } } else { MessageBox.Show("This File Already Exists. If You Want To Edit This File Click On Edit."); } } }
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length == 0) { OpenFileDialog fileDialog = new OpenFileDialog(); DialogResult dialogResult = fileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { string nameAndExtension = fileDialog.SafeFileName; lastChosenName = nameAndExtension.Remove(nameAndExtension.Length - 4, 4); textBox1.Text = lastChosenName; lastChosenPath = fileDialog.FileName; fileDialog.Dispose(); DialogResult result = MessageBox.Show("Would you like to select the categories (Click Yes) or do you need category suggestions (Click No)?", "Warning", MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.Yes) { MessageBox.Show("Please select the categories then click on 'Add an existing file' again."); return; } else { MessageBox.Show("Please select categories from the suggestions then click on 'Add an existing file' again."); catCheckList.Items.Clear(); Categories_Name.Clear(); FileStream fs = new FileStream(lastChosenPath, FileMode.Open); StreamReader sr = new StreamReader(fs); while (sr.Peek() != -1) { string line = sr.ReadLine(); string[] words = line.Split(' ', ',', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '+', '=', '~'); foreach (string word in words) { foreach (KeyValuePair <string, Category> KVP in Home.mapOfCategories) { if (catCheckList.Items.Contains(KVP.Key)) { continue; } foreach (string keyword in KVP.Value.keywords) { if (word == keyword) { catCheckList.Items.Add(KVP.Key); Categories_Name.Add(KVP.Key); break; } } } } } } } } else { bool fg = false; for (int i = 0; i < catCheckList.Items.Count; i++) { if (catCheckList.GetItemCheckState(i) == CheckState.Checked) { fg = true; break; } } if (fg == false) { MessageBox.Show("You have to select category/ies. Please retry."); return; } if (lastChosenPath.Length == 0 || lastChosenName.Length == 0) { MessageBox.Show("You have to select a file first."); return; } //string path = @"C:\Users\Menna\Source\Repos\File-Search-Engine-System\Files\"; string Url = lastChosenPath; List <string> categories = new List <string>(); XmlDocument doc = new XmlDocument(); XmlElement FileInfo = doc.CreateElement("FileInfo"); XmlElement node = doc.CreateElement("FileName"); node.InnerText = lastChosenName; FileInfo.AppendChild(node); node = doc.CreateElement("Path"); node.InnerText = Url; FileInfo.AppendChild(node); node = doc.CreateElement("Categories"); bool f = false; string node_text = ""; for (int i = 0; i < catCheckList.Items.Count; i++) { if (catCheckList.GetItemCheckState(i) == CheckState.Checked) { if (f == false) { node_text += Categories_Name[i]; node_text += " "; f = true; } else { node_text += Categories_Name[i]; node_text += " "; } categories.Add(Categories_Name[i]); } } node.InnerText = node_text; FileInfo.AppendChild(node); doc.Load("TextFiles.xml"); XmlElement root = doc.DocumentElement; root.AppendChild(FileInfo); doc.Save("TextFiles.xml"); FILE ff = new FILE(); ff.fileName = lastChosenName; ff.path = lastChosenPath; Home.mapOfFiles[ff.fileName] = ff; foreach (string cat in categories) { Home.mapOfFiles[ff.fileName].fileCategories.Add(cat); } fileCombo.Items.Add(ff.fileName); fileCombo.SelectedIndex = fileCombo.Items.Count - 1; textBox1.Clear(); lastChosenName = ""; lastChosenPath = ""; catCheckList.Items.Clear(); Categories_Name.Clear(); foreach (KeyValuePair <string, Category> KVP in Home.mapOfCategories) { catCheckList.Items.Add(KVP.Key); Categories_Name.Add(KVP.Key); } } }