//--------------- Add Files -----------------------------// private void btnAddFile_Click(object sender, EventArgs e) { lblFiles.Text = ""; lblRefresh.Text = ""; openFileDialog1.Reset(); // Get the InitialDirectory to Open if ComboBoxDir is selected string selectedSubDir = (String)comboBoxSubDir.SelectedItem; string selectedDir = (String)comboBoxDir.SelectedItem; // open Directoryy.xml file To read the full subdirectory match selectedSubDir to use after in openFileDialog1 List <Directoryy> list = new List <Directoryy>(); XmlSerializer serializer = new XmlSerializer(typeof(List <Directoryy>)); List <Filess> list2 = new List <Filess>(); XmlSerializer serializer2 = new XmlSerializer(typeof(List <Filess>)); using (FileStream fs = new FileStream("C:\\Users\\Public\\Documents" + "\\Directories.xml", FileMode.Open, FileAccess.Read)) { list = serializer.Deserialize(fs) as List <Directoryy>; } foreach (Directoryy listElement in list) { string[] pathArr0 = listElement.NameDir.Split('\\'); string element = pathArr0[pathArr0.Length - 1]; string parentelement = pathArr0[pathArr0.Length - 2]; if (listElement.TypeDir == "subdirectory" && String.Equals(selectedSubDir, element, StringComparison.Ordinal) && String.Equals(selectedDir, parentelement, StringComparison.Ordinal)) { openFileDialog1.InitialDirectory = listElement.NameDir; // Set Default Directory To OpenFileDialog } } openFileDialog1.Multiselect = false; // Don't Allow Dialog to select multi file openFileDialog1.ShowDialog(); string path = openFileDialog1.FileName; // File Selected by openFileDialog1 if (path != null) { string namefile = Path.GetFileName(path); // Path.GetFileNameWitoutExtension string[] pathArr = path.Split('\\'); //string subdir = pathArr[pathArr.Length - 2]; // old value used string subdir = Path.GetDirectoryName(path); // full sub directory name if (dataGridView1.Rows.Count == 0) { // dataGridView1 is Empty, You can Add without verification dataGridView1.Rows.Add(namefile, subdir, new DataGridViewButtonCell(), "pas de Fichier Signé", false); // Create Filess element to add in list2 string namefile2 = dataGridView1.Rows[0].Cells[0].Value.ToString(); string subdirfile = dataGridView1.Rows[0].Cells[1].Value.ToString(); bool realized = (bool)dataGridView1.Rows[0].Cells[4].Value; string realizedfile = dataGridView1.Rows[0].Cells[3].Value.ToString(); Filess file2 = new Filess() { NameFile = namefile, SubDirFile = subdirfile, StateFile = realized, RealizedFile = realizedfile }; // open Files.xml => list2 => add new element to list2 ==> export to Files.xml // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // add element to list2 list2.Add(file2); // export list2 updated to Files.xml using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.OpenOrCreate, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } lblFiles.Text = "Fichier ajouté avec succès"; // read only the file selected lblFiles.ForeColor = System.Drawing.Color.Green; } else { // You must verifying if the name of file exist or not List <string> list_filename = new List <string>(); List <string> list_subdir = new List <string>(); foreach (DataGridViewRow row in dataGridView1.Rows) { list_filename.Add(row.Cells[0].Value.ToString()); // list_filename contains all of the names in the Firt Column in dataGridView1 list_subdir.Add(row.Cells[1].Value.ToString()); // list_subdir contains all subdirectory related to filename in dataGridView1 } // files from one subdirectory selected if (list_subdir.TrueForAll(i => i.Equals(list.FirstOrDefault())) /* && String.Equals(list_subdir.First(),subdir, StringComparison.Ordinal)*/) { if (list_filename.Contains(namefile) == false) { dataGridView1.Rows.Add(namefile, subdir, new DataGridViewButtonCell(), "pas de Fichier Signé", false); // open File.xml in Read mode ==> create list2 ==> add new element to list2 ==> open Files.xml in Write mode string namefile2 = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value.ToString(); string subdirfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value.ToString(); bool realized = (bool)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[4].Value; string realizedfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value.ToString(); Filess file2 = new Filess() { NameFile = namefile, SubDirFile = subdirfile, StateFile = realized, RealizedFile = realizedfile }; // open Files.xml => list2 => add new element to list2 ==> export to Files.xml // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // add element to list2 list2.Add(file2); // export list2 updated to Files.xml using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.OpenOrCreate, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } lblFiles.Text = "Fichier ajouté avec succès"; lblFiles.ForeColor = System.Drawing.Color.Green; } else { lblFiles.Text = "Le Fichier existe déjà"; lblFiles.ForeColor = System.Drawing.Color.Red; } } // All File from all sub directory else { if (list_filename.Contains(namefile)) // Verify if subdir equals to list_subdir element for the same index of list_filename { var index1 = list_filename.IndexOf(namefile); if (String.Equals(list_subdir[index1], subdir, StringComparison.Ordinal)) { lblFiles.Text = "Le Fichier existe dans le dossier " + subdir; lblFiles.ForeColor = System.Drawing.Color.Red; } else { dataGridView1.Rows.Add(namefile, subdir, new DataGridViewButtonCell(), "pas de Fichier Signé", false); // open File.xml in Read mode ==> create list2 ==> add new element to list2 ==> open Files.xml in Write mode string namefile2 = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value.ToString(); string subdirfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value.ToString(); bool realized = (bool)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[4].Value; string realizedfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value.ToString(); Filess file2 = new Filess() { NameFile = namefile, SubDirFile = subdirfile, StateFile = realized, RealizedFile = realizedfile }; // open Files.xml => list2 => add new element to list2 ==> export to Files.xml // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // add element to list2 list2.Add(file2); // export list2 updated to Files.xml using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.OpenOrCreate, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } lblFiles.Text = "Fichier ajouté avec succès"; lblFiles.ForeColor = System.Drawing.Color.Green; } } // Add element directly in dataGridView1 and Files.xml else { dataGridView1.Rows.Add(namefile, subdir, new DataGridViewButtonCell(), "pas de Fichier Signé", false); // open File.xml in Read mode ==> create list2 ==> add new element to list2 ==> open Files.xml in Write mode string namefile2 = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value.ToString(); string subdirfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value.ToString(); bool realized = (bool)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[4].Value; string realizedfile = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value.ToString(); Filess file2 = new Filess() { NameFile = namefile, SubDirFile = subdirfile, StateFile = realized, RealizedFile = realizedfile }; // open Files.xml => list2 => add new element to list2 ==> export to Files.xml // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // add element to list2 list2.Add(file2); // export list2 updated to Files.xml using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.OpenOrCreate, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } lblFiles.Text = "Fichier ajouté avec succès"; lblFiles.ForeColor = System.Drawing.Color.Green; } } } } else { MessageBox.Show("Veuillez Choisir un Fichier", "Choix", MessageBoxButtons.OK); // To modify } }
//--------------- Delete Directory -----------------------------// // Delete Directory or SubDirectory : if subdirectory ==> delete automatically private void button7_Click(object sender, EventArgs e) { lblDir.Text = ""; if (MessageBox.Show("Voulez-vous supprimer les dossiers ?", "Suppression de dossiers", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == DialogResult.Yes) { // Get the Selected Item in ListBox and get only the name of directory namedir string dirselected = listBoxDir.SelectedItem.ToString(); string[] pathArr = dirselected.Split('\\'); string namedir = pathArr[pathArr.Length - 1]; //********** search if namedir exist in list ********* // open File to read content and place that in list List <Directoryy> list = new List <Directoryy>(); XmlSerializer serializer = new XmlSerializer(typeof(List <Directoryy>)); List <Filess> list2 = new List <Filess>(); XmlSerializer serializer2 = new XmlSerializer(typeof(List <Filess>)); using (FileStream fs = new FileStream("C:\\Users\\Public\\Documents" + "\\Directories.xml", FileMode.Open, FileAccess.Read)) { list = serializer.Deserialize(fs) as List <Directoryy>; } // Fetch Selected Directory "dirselected" List <string> list_NameDir = new List <string>(); List <string> list_TypeDir = new List <string>(); for (int i = 0; i < list.Count; i++) { list_NameDir.Add(list.ElementAt(i).NameDir); list_TypeDir.Add(list.ElementAt(i).TypeDir); } if (list_NameDir.Contains(dirselected)) { int indexDir = list_NameDir.IndexOf(dirselected); // index of selected directory into list => into Directories.xml if (String.Equals(list_TypeDir[indexDir], "directory", StringComparison.Ordinal)) { //MessageBox.Show("selected directory: " + list_NameDir.ElementAt(indexDir) + " Type: " + list_TypeDir[indexDir]); // ----- search in ListBox the subdirectories of selected directory --- // List <string> list1 = new List <string>(); for (int i = 0; i < listBoxDir.Items.Count; i++) { string[] pathArr3 = listBoxDir.Items[i].ToString().Split('\\'); string dirItemparent = pathArr3[pathArr3.Length - 2]; if (String.Equals(namedir, dirItemparent, StringComparison.Ordinal)) { list1.Add(listBoxDir.Items[i].ToString()); // Add all subdirectory related to selected directory in list1 //MessageBox.Show("sub-directory to be deleted: " + listBoxDir.Items[i].ToString()); } } //Remove files from Files.xml that his the same subdirectory to remove // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // Fill list_subdirectory from list2 List <string> list_subdirectory = new List <string>(); for (int i = 0; i < list2.Count; i++) { list_subdirectory.Add(list2.ElementAt(i).SubDirFile); } // Loop into list_subdirectory and fetch if each element are the same value in list1 List <int> listindex = new List <int>(); for (int i = 0; i < list_subdirectory.Count; i++) { if (list1.Contains(list_subdirectory[i])) { listindex.Add(i); //MessageBox.Show("Name File: " +list2.ElementAt(i).NameFile); } } List <Filess> listfileToDelete = new List <Filess>(); for (int i = 0; i < listindex.Count; i++) { //MessageBox.Show("index File: " + listindex.ElementAt(i)); Filess filess = list2.ElementAt(i + listindex[0]); listfileToDelete.Add(filess); //MessageBox.Show("Added file: " + filess.NameFile); } foreach (Filess file in listfileToDelete) { list2.Remove(file); //MessageBox.Show("files removed successfully ! "); } // open Files.xml in Write Mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Create, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } // remove selected directory and all related subdirectories from ListBoxDir list1.Add(listBoxDir.SelectedItem.ToString()); foreach (string element1 in list1) { for (int i = 0; i < listBoxDir.Items.Count; i++) { if (String.Equals(element1, listBoxDir.Items[i].ToString(), StringComparison.Ordinal)) { listBoxDir.Items.Remove(listBoxDir.Items[i]); } } } //listBoxDir.Items.Remove(comboBoxDir.SelectedItem); FillDataGridView1(); // -----------------------------------------------------// lblDir.Text = "Le Dossier et leur fichiers ont été supprimé"; lblDir.ForeColor = System.Drawing.Color.Red; } else if (String.Equals(list_TypeDir[indexDir], "subdirectory", StringComparison.Ordinal)) { // open Files.xml in Read mode using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Open, FileAccess.Read)) { list2 = serializer2.Deserialize(fs2) as List <Filess>; } // Fill list_subdirectory from list2 List <string> list_subdirectory = new List <string>(); for (int i = 0; i < list2.Count; i++) { list_subdirectory.Add(list2.ElementAt(i).SubDirFile); } // Loop into list_subdirectory and fetch if each element are the same value of dirselected List <int> listindex = new List <int>(); for (int i = 0; i < list_subdirectory.Count; i++) { if (String.Equals(list_subdirectory[i], dirselected, StringComparison.Ordinal)) { listindex.Add(i); //MessageBox.Show("File index= "+ i +" Name File: " + list2.ElementAt(i).NameFile); } } List <Filess> listfileToDelete = new List <Filess>(); for (int i = 0; i < listindex.Count; i++) { //MessageBox.Show("index File: " + listindex.ElementAt(i)); Filess filess = list2.ElementAt(i + listindex[0]); listfileToDelete.Add(filess); //MessageBox.Show("Added file: " + filess.NameFile); } foreach (Filess file in listfileToDelete) { list2.Remove(file); //MessageBox.Show("files removed successfully ! "); } using (FileStream fs2 = new FileStream("C:\\Users\\Public\\Documents" + "\\Files.xml", FileMode.Create, FileAccess.Write)) { serializer2.Serialize(fs2, list2); } listBoxDir.Items.Remove(listBoxDir.SelectedItem); //listBoxDir.Items.Remove(comboBoxDir.SelectedItem); FillDataGridView1(); // -----------------------------------------------------// lblDir.Text = "Le Sous dossier et leur fichiers ont été supprimé"; lblDir.ForeColor = System.Drawing.Color.Red; } } else { MessageBox.Show("Pas de dossier à supprimer"); } } else { } button5_Click(sender, e); }