Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. listing files in C#

listing files in C#

Scheduled Pinned Locked Moved C#
csharpxml
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    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;
    
    M 1 Reply Last reply
    0
    • M mrithula8

      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;
      
      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups