2 Replies - 23021 Views - Last Post: 21 October 2010 - 05:54 AM Rate Topic: -----

#1 Sparukus   User is offline

  • D.I.C Head

Reputation: -11
  • View blog
  • Posts: 190
  • Joined: 05-April 10

Add files from directory to listbox.

Posted 12 October 2010 - 02:17 PM

Hello,

Trying to add files from a directory on the local machine to fill a listbox atm I tryed using a wild card with *.* but when ever I run it the listbox doesnt seem to be showing the list of files in my folder.

Example Codes Ive tryed in different locations:
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

          //  DirectoryInfo info;

           // info = new DirectoryInfo(@"C:\Users\Garrith-Super-PC\Desktop\New folder");

           // listBox1.Datsource = info.Getfiles();

           // listBox1.Databind();

            string[] files = System.IO.Directory.GetFiles(@"C:\Users\Garrith-Super-PC\Desktop\New folder");

            foreach (string file in files)
            {

                listBox1.Items.Add(file);

            }
        }


and:

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Garrith-Super-PC\Desktop\New folder");
            FileInfo[] Files = dinfo.GetFiles("*.");
            foreach (FileInfo file in Files)
            {
                listBox1.Items.Add(file.Name);
            }


I have also tryed using the filesystemwatcher but its kind of rubbish it only shows me which files have been moved deleted etc only once ive did something rather than showing me whats there to start off with.

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was changed !");
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was created !");
        }

        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was deleted !");
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was renamed !");
        }


it works but just isnt what I want. Could some one point me in the right direction?

Thanks

G

Is This A Good Question/Topic? 0
  • +

Replies To: Add files from directory to listbox.

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Add files from directory to listbox.

Posted 21 October 2010 - 04:48 AM

This code works perfectly fine for me.

// I have a "Test" directory on the root of my C: drive
string[] files = Directory.GetFiles(@"C:\test");

foreach (string file in files)
{
    listBox1.Items.Add(file);
}



I have 16 files in that directory and all 16 how up in my ListBox.

What does your's do?
Was This Post Helpful? 0
  • +
  • -

#3 fixo   User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Add files from directory to listbox.

Posted 21 October 2010 - 05:54 AM

View PostSparukus, on 12 October 2010 - 01:17 PM, said:

Hello,

Trying to add files from a directory on the local machine to fill a listbox atm I tryed using a wild card with *.* but when ever I run it the listbox doesnt seem to be showing the list of files in my folder.

Example Codes Ive tryed in different locations:
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

          //  DirectoryInfo info;

           // info = new DirectoryInfo(@"C:\Users\Garrith-Super-PC\Desktop\New folder");

           // listBox1.Datsource = info.Getfiles();

           // listBox1.Databind();

            string[] files = System.IO.Directory.GetFiles(@"C:\Users\Garrith-Super-PC\Desktop\New folder");

            foreach (string file in files)
            {

                listBox1.Items.Add(file);

            }
        }


and:

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Garrith-Super-PC\Desktop\New folder");
            FileInfo[] Files = dinfo.GetFiles("*.");
            foreach (FileInfo file in Files)
            {
                listBox1.Items.Add(file.Name);
            }


I have also tryed using the filesystemwatcher but its kind of rubbish it only shows me which files have been moved deleted etc only once ive did something rather than showing me whats there to start off with.

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was changed !");
        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was created !");
        }

        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was deleted !");
        }

        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            listBox1.Items.Add(e.FullPath + " was renamed !");
        }


it works but just isnt what I want. Could some one point me in the right direction?

Thanks

G


Your woldcard pattern is wrong
Instead of
FileInfo[] Files = dinfo.GetFiles("*.");

try this one
FileInfo[] Files = dinfo.GetFiles("*.*");


~'J'~
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1