?
Solved

Populate a ListBox with File Names from a Directory

Posted on 2013-11-18
3
Medium Priority
?
834 Views
Last Modified: 2013-11-19
I have a listbox named lstFiles. and a directory named C:\MyApp\MyFiles.

The directory contains 12 files whose extension is .txt.

How can I populate the listbox with file names in alphabetical order?

Thanks,
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 39658230
*Assuming WinForms as this really doesn't sound like ASP.Net...

Try something like this:
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\MyApp\MyFiles";
            var files = (from file in System.IO.Directory.GetFiles(path, "*.txt")
                         orderby System.IO.Path.GetFileNameWithoutExtension(file) ascending 
                         select new
                         {
                             Name = System.IO.Path.GetFileNameWithoutExtension(file),
                             FullPath = file
                         }).ToArray();
            lstFiles.DisplayMember = "Name";
            lstFiles.ValueMember = "FullPath";
            lstFiles.DataSource = files;
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstFiles.SelectedIndex != -1)
            {
                label1.Text = lstFiles.SelectedValue.ToString();
            }
        }

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39660187
This is working for asp.net in a web form.

files[2] returns

{ Name = "AMEX_20131017", FullPath = "C:\\Develop\\Apps\\StockPro\\Data\\AMEX_20131017.txt" }
    FullPath: "C:\\Develop\\Apps\\StockPro\\Data\\AMEX_20131017.txt"
    Name: "AMEX_20131017"

Thanks
0
 

Author Closing Comment

by:Dovberman
ID: 39660189
Perfect.

Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question