Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List files from directory

Status
Not open for further replies.

hassified

Technical User
Jun 25, 2002
43
US
Hello all,
I'm still new to c# and am learning alot, so this question is most likly pretty stupid to you guys but its all learning to me.
Question, how do I create a scrollable listing of files from a directory? One that automaticly generates the list from a directory without file extensions. Is it possible?

In case you need to know I'm using Visual C# 2005 Express.

Thanks in advance.

Life Is Good.
HASSIFIED
 
hassified,

Something like the following should get you what you need. (code assumes a form with a button named button1 and a listbox named listBox1 on it)
Code:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo("c:\\");
            FileInfo[] rgFiles = di.GetFiles("*.*");
            foreach (FileInfo fi in rgFiles)
            {
                listBox1.Items.Add(Path.GetFileNameWithoutExtension(fi.Name));
            }

        }
    }
}

boyd.gif

SweetPotato Software Website
My Blog
 
Thank you very much, so if i wanted it to show files in list box of a directory called images it would look like this?

DirectoryInfo di = new DirectoryInfo("\images\");

And also, do I have to have a button?

I'm wanting it automaticly pull files from this directory called "images". I'm experminting with the idea of making an app that will run from a cd and let the user click a file name in the list box and will show image in the app.

Is C# the right language to use???

Life Is Good.
HASSIFIED
 
DirectoryInfo di = new DirectoryInfo("\images\");
You should specify a full path.

And also, do I have to have a button?
No - the button_click event was just there to get the code to execute. You can put it in it's own method and call it from nearly anywhere.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top