Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:24:38+00:00 2026-06-16T02:24:38+00:00

I have a list box and its populated by this method, private void ToReadFromExcel_Load(object

  • 0

I have a list box and its populated by this method,

private void ToReadFromExcel_Load(object sender, EventArgs e)
{
    string folderpath = @"\\gibson\users";
    // Call the method to show available files
    PopulateListBox(ExcelListBox, folderpath, "*.csv");
}

// To populate list box with csv files from given path
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
    DirectoryInfo dinfo = new DirectoryInfo(Folder);
    FileInfo[] Files = dinfo.GetFiles(FileType);
    foreach (FileInfo file in Files)
    {
        lsb.Items.Add(file.Name);
    }
}

String strItem;
foreach (Object selecteditem in ExcelListBox.SelectedItems)
{
    strItem = selecteditem as String;
    MessageBox.Show(strItem);
}
// read csv file information and insert into detail table
string filepath = @"\\gibson\users\CampManager.csv";
StreamReader sr = new StreamReader(filepath);

I hard coded the file path now, but I need to pass the filepath that was selected in the listbox. I have the file name in the variable stritem. If I want to pass the whole folder path how would I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:24:39+00:00Added an answer on June 16, 2026 at 2:24 am

    There is an ideal way. You should, instead of adding the FileInfo object’s Name, should add the FileInfo object itself. So later you will be able to retrieve any piece of info related to that object, in your case say size, parent folder etc, and not just file name. Do it like this:

    // To populate list box with csv files from given path
    private void PopulateListBox(ListBox lsb, string Folder, string FileType)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Folder);
        FileInfo[] Files = dinfo.GetFiles(FileType);
        foreach (FileInfo file in Files)
        {
            lsb.Items.Add(file); //<-- note here
        }
    }
    
    String strItem;
    foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
    {
         StreamReader sr = new StreamReader(selecteditem.FullName);
         //or whatever
    }
    

    One thing you should take care of here is to set the DisplayMember property of the ListBox like this:

    ExcelListBox.DisplayMember = "Name";
    

    What this does is to set the what property of the object in the listbox should be displayed. So here you choose FileInfo.Name which is what you want. This is how typically custom objects are added to ListBoxes in WinForms. You do not add just the string part of it typically. Akin to DisplayMember, there is also ValueMember property which is used to assign a value for each object, probably some id or so, but in your case nothing.

    Few suggestions, 1) If you’re using .NET 4, then use EnumerateFiles instead of GetFiles. The former is lazy and only yields a result when you start enumerating them (not beforehand), so it should be faster.

    foreach (FileInfo file in dinfo.EnumerateFiles(FileType)) //<-- note here
    {
        lsb.Items.Add(file); 
    }
    

    2) Use a using clause to dispose your stream reader properly, since that wont lock your file. It’s always a good practice to make use of using. Its better on eyes than manual closing and disposing! Like this or so:

    foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
    {
         using(StreamReader sr = new StreamReader(selecteditem.FullName))
         {
             //your code here
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list box like this, <asp:ListBox ID=ListBox1 runat=server Height=175px Width=213px> <asp:ListItem Value=all>All</asp:ListItem>
I have a List Box that implements its ItemsPanelTemplate as a WrapPanel. I need
[Original] I have a ListBox which has its ItemsSource (this is done in the
I have a list box control that contains enough items to list them with
I have the list box control (ASP.NET Control On aspx page, language C# ).
i have a list box and i want to add a folder/directory to that
I have a list box and i am trying to get currently checked item
I want to have a list box in JSF. I have written a simple
I have a list box that I'm trying to populate with a list of
I have a list view, the contain is populated from a database. And I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.