[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Populate a ListBox with File Names from a Directory

Posted on 2013-11-18
3
Medium Priority
?
861 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

656 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