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 7906003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:43:25+00:00 2026-06-03T10:43:25+00:00

I have a listbox and it has some files loaded from a directory folder.

  • 0

I have a listbox and it has some files loaded from a directory folder.

Code to load the files into the listBox1:

private void Form1_Load(object sender, EventArgs e)
        {
            PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld");

        }

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);
            }
        }

I want to read and display the attributes values to the labels in the form. The loaded files in the listBox1, here is the code:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    string path = (string)listBox1.SelectedItem;
    DisplayFile(path);
} 
private void DisplayFile(string path)
{
    string xmldoc = File.ReadAllText(path);

    using (XmlReader reader = XmlReader.Create(xmldoc))
    {   

        while (reader.MoveToNextAttribute())
        {
          switch (reader.Name)
          {
            case "description":
              if (!string.IsNullOrEmpty(reader.Value))
                label5.Text = reader.Value; // your label name
              break;
            case "sourceId":
              if (!string.IsNullOrEmpty(reader.Value))
                label6.Text = reader.Value; // your label name
              break;
            // ... continue for each label
           }
        }
    }
} 

Problem:When I click on the file in the listBox1 after the form is loaded,the files are loaded from the folder into the listbox but it’s throwing an error File not found in the directory.

How can I fix this problem???

  • 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-03T10:43:26+00:00Added an answer on June 3, 2026 at 10:43 am

    That is because you are adding File.Name instead you should add File.FullName in your listbox

    lsb.Items.Add(file.FullName);
    

    so your method PopulateListBox should become:

    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.FullName);
                }
            }
    

    EDIT:
    It looks like you want to display only the file name, not the full path. You could follow a following approach. In PopulateListBox, add file instead of file.FullName so the line should be

    foreach (FileInfo file in Files)
                {
                    lsb.Items.Add(file);
                }
    

    Then in SelectedIndexChanged event do the following:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                FileInfo file = (FileInfo)listbox1.SelectedItem;
                DisplayFile(file.FullName);
             }
    

    This should get you the full name (file name with path) and will resolve your exception of File Not Found

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox & it has some files. I have 2 Panels in
I have a page that has several ListBox es that have some cascading filtering
I currently have a listbox that has its selected item bound to a property
[Original] I have a ListBox which has its ItemsSource (this is done in the
I am new to WPF. I have a ListBox that has its ItemSource set
I have a ListBox in Silverlight that has a list of items inside. Each
So I have a form that has a listbox that shows like a ledger.
I have a task killer program that has a listbox of current running processes.
I currently have a listbox in wpf c# and added some keybindings to the
I have a listbox that has a datatemplate which contains multiple text boxes. 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.