listing files in C#
-
Hi I used the following code for listing files
int h=listBox1.SelectedIndex;
if(h==0)
{
try
{
reader = new XmlTextReader("path.xml");while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: //Display the text in each element. // Console.WriteLine(reader.Value); String s1; s1 = reader.Value; String ss; ss = listBox1.SelectedItem.ToString(); sss = ss + s1; object o3 = (object)sss; DirectoryInfo dire = new DirectoryInfo(sss); //DirectoryInfo\[\] dirs = dire.GetDirectories(); if (dire.Exists) { String\[\] folder; folder = Directory.GetDirectories(sss); foreach (string foldername in folder) { DirectoryInfo di = new DirectoryInfo(foldername); if (di.Exists) { String\[\] files; files = Directory.GetFiles(foldername, "\*.exe\*"); //FileInfo\[\] fi = dire.GetFiles(); foreach (String filename in files) { lvi = new ListViewItem(); lvi.Text = filename; listView1.Items.Add(lvi); lvsi = new ListViewItem.ListViewSubItem(); lvsi.Text = filename.Length.ToString(); lvi.SubItems.Add(lvsi); } this.Controls.Add(listView1); } } } break;
-
Hi I used the following code for listing files
int h=listBox1.SelectedIndex;
if(h==0)
{
try
{
reader = new XmlTextReader("path.xml");while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Text: //Display the text in each element. // Console.WriteLine(reader.Value); String s1; s1 = reader.Value; String ss; ss = listBox1.SelectedItem.ToString(); sss = ss + s1; object o3 = (object)sss; DirectoryInfo dire = new DirectoryInfo(sss); //DirectoryInfo\[\] dirs = dire.GetDirectories(); if (dire.Exists) { String\[\] folder; folder = Directory.GetDirectories(sss); foreach (string foldername in folder) { DirectoryInfo di = new DirectoryInfo(foldername); if (di.Exists) { String\[\] files; files = Directory.GetFiles(foldername, "\*.exe\*"); //FileInfo\[\] fi = dire.GetFiles(); foreach (String filename in files) { lvi = new ListViewItem(); lvi.Text = filename; listView1.Items.Add(lvi); lvsi = new ListViewItem.ListViewSubItem(); lvsi.Text = filename.Length.ToString(); lvi.SubItems.Add(lvsi); } this.Controls.Add(listView1); } } } break;
I think you need to re-think you design. And please use more descriptive variable names. and h can never be both 0 and 1, so your two if statements need to be and else if, or a swicth, or even better get rid of them completely as you never use h in your code so its value has no matter in the output you get. I am assuming you have a list box with two entries. One c:/ and one d:/, correct? if so then ss (which i assume is getting the drive letter) is not working as you expect, try debuging it with a writeline or messagebox showing its value. if you can confirm this then you can move to the problem being else where.
Life goes very fast. Tomorrow, today is already yesterday.