mounika ymr

mounika ymr

  • NA
  • 90
  • 33.7k

how to generate a checkbox list files and files in subfolder

Sep 26 2017 2:35 PM
I’ve generated winform which will display a list of files in a folder and checked items generate xml file. Now, I wanted to generate a list of files and files in subfolder which should be listed in checkbox list whatever the files I checked will get generate a xml file with file path(as parent) and file name (child) separated as parent and child node, and un checked files also should generate xml file as separate node. Will u please modify my code below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.IO;  
  11. using System.Xml;  
  12. namespace GetDetails  
  13. {  
  14. public partial class Form1 : Form  
  15. {  
  16. //List listfiles = new List();  
  17. public Form1()  
  18. {  
  19. InitializeComponent();  
  20. }  
  21. private void textBox1_TextChanged(object sender, EventArgs e)  
  22. {  
  23. //string path;  
  24. //path = FolderBrowserDialog.Equals("");  
  25. //textBox1.Text = FolderBrowserDialog.SelectedPath;  
  26. ////path = folderBrowserDialog1.SelectedPath;  
  27. //// folderBrowserDialog1.ShowDialog(); // NOT SURE ABOUT USING THIS!  
  28. //textBox1.Text = path;  
  29. }  
  30. private void button4_Click(object sender, EventArgs e)  
  31. {  
  32. // Create a new instance of FolderBrowserDialog.  
  33. FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();  
  34. // A new folder button will display in FolderBrowserDialog.  
  35. folderBrowserDlg.ShowNewFolderButton = true;  
  36. //Show FolderBrowserDialog  
  37. DialogResult dlgResult = folderBrowserDlg.ShowDialog();  
  38. if (dlgResult.Equals(DialogResult.OK))  
  39. {  
  40. //Show selected folder path in textbox1.  
  41. textBox1.Text = folderBrowserDlg.SelectedPath;  
  42. //Browsing start from root folder.  
  43. Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;  
  44. }  
  45. }  
  46. private void button1_Click(object sender, EventArgs e)  
  47. {  
  48. FolderBrowserDialog fbd = new FolderBrowserDialog();  
  49. if (fbd.ShowDialog() == DialogResult.OK)  
  50. {  
  51. if (!textBox1.Text.Equals(String.Empty))  
  52. {  
  53. if (System.IO.Directory.GetFiles(textBox1.Text).Length > 0)  
  54. {  
  55. foreach (string file in System.IO.Directory.GetFiles(textBox1.Text))  
  56. {  
  57. //Add file in ListBox.  
  58. checkedListBox1.Items.Add(Path.GetFileName(file));  
  59. }  
  60. }  
  61. else  
  62. {  
  63. checkedListBox1.Items.Add(String.Format("No files Found at location: { 0}", textBox1.Text));  
  64. }  
  65. }  
  66. /* 
  67. FolderBrowserDialog fbd = new FolderBrowserDialog(); 
  68. if(fbd.ShowDialog() == DialogResult.OK) 
  69. { 
  70. checkedListBox1.Items.Clear(); 
  71. string[] files = Directory.GetFiles(fbd.SelectedPath); 
  72. string[] dirs = Directory.GetDirectories(fbd.SelectedPath); 
  73. foreach(string file in files) 
  74. { 
  75. checkedListBox1.Items.Add(Path.GetFileName(file)); 
  76. } 
  77. foreach (string dir in dirs) 
  78. { 
  79. checkedListBox1.Items.Add(Path.GetFileName(dir)); 
  80. } 
  81. }*/  
  82. }  
  83. }  
  84. private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)  
  85. {  
  86. }  
  87. private void button2_Click(object sender, EventArgs e)  
  88. {  
  89. string str = " ";  
  90. int i;  
  91. for (i = 0; i < checkedListBox1.Items.Count; i++)  
  92. {  
  93. if (checkedListBox1.GetItemChecked(i))  
  94. {  
  95. str = (string)checkedListBox1.Items[i];  
  96. MessageBox.Show(str);  
  97. //xwriter.WriteElementString("SelectedItem", checkedListBox1.Items[i].ToString());  
  98. }  
  99. }  
  100. //System.IO.File.WriteAllText("GetDetails.xml", str);  
  101. XmlTextWriter xwriter = new XmlTextWriter("GetDetails.xml", Encoding.Unicode);  
  102. xwriter.WriteStartDocument();  
  103. xwriter.WriteStartElement("XMLFILE");  
  104. xwriter.WriteStartElement("file");  
  105. xwriter.WriteString(textBox1.Text);  
  106. xwriter.WriteEndElement();  
  107. foreach (var item in checkedListBox1.CheckedItems)  
  108. {  
  109. xwriter.WriteStartElement("IncludedItems");  
  110. xwriter.WriteString(item.ToString());  
  111. xwriter.WriteEndElement();  
  112. }  
  113. xwriter.WriteEndElement();  
  114. xwriter.WriteEndDocument();  
  115. xwriter.Close();  
  116. }  
  117. private void button3_Click(object sender, EventArgs e)  
  118. {  
  119. this.Close();  
  120. }  
  121. private void Form1_Load(object sender, EventArgs e)  
  122. {  
  123. }  
  124. private void button5_Click(object sender, EventArgs e)  
  125. {  
  126. this.Close();  
  127. }  
  128. }  
  129. }

Answers (1)