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

  • Home
  • SEARCH
  • 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 504259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:42+00:00 2026-05-13T06:28:42+00:00

What I have is a comboBox being populated from a code: InitializeComponent(); DirectoryInfo dinfo

  • 0

What I have is a comboBox being populated from a code:

InitializeComponent();
        DirectoryInfo dinfo = new DirectoryInfo(@"K:\ases");
        FileInfo[] Files = dinfo.GetFiles("*.ssi", SearchOption.AllDirectories);
        foreach (FileInfo file in Files)
        {
            comboBox1.Items.Add(file.Name);
        }

Then i’m trying to ReadAllText from that selected item.

    string contents = File.ReadAllText(comboBox1.Text);

When executed, it tries to read the file from the locale path, and of course the file isnt there. I also can’t set the working directory because the comoboBox is populated from a huge range of subdirectories. How do I get the working directory of the item selected in the combobox WITHOUT expossing the whole directory path to the user?

Any help always welcomed

I thought i saw a few answers with someone suggesting private and hiding things in a combox box, where those suggestions taken down. is there a way to keep the full file info in the combobox and only display the file name?

  • 1 1 Answer
  • 2 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-05-13T06:28:42+00:00Added an answer on May 13, 2026 at 6:28 am

    There are several different ways to accomplish this, but the absolute easiest would be to store the directory path in an instance variable (we’ll call it directoryPath) and use System.IO.Path.Combine() to reconstruct the path:

    (some code eliminated for brevity)

    public class Form1 : Form
    {
        private string directoryPath;
    
        public Form1()
        {
            InitializeComponent();
            DirectoryInfo dinfo = new DirectoryInfo(@"K:\ases");
            FileInfo[] Files = dinfo.GetFiles("*.ssi", SearchOption.AllDirectories);
            foreach (FileInfo file in Files)
            {
                comboBox1.Items.Add(file.Name);
            }
    
            directoryPath = dinfo.FullName;
        }
    
        private void YourFunction()
        {
            string contents = File.ReadAllText(System.IO.Path.Combine(directoryPath, comboBox1.Text);
        }
    }
    

    You could also try adding the FileInfo to the ComboBox rather than the file’s name and use SelectedItem rather than Text:

        InitializeComponent();
    
        DirectoryInfo dinfo = new DirectoryInfo(@"K:\ases");
        comboBox1.DataSource = dinfo.GetFiles("*.ssi", SearchOption.AllDirectories);
    
        comboBox1.DisplayMember = "Name";
    

    Then you can do this to retrieve the file:

    FileInfo file = (FileInfo)comboBox1.SelectedItem;
    
    string contents = File.ReadAllText(file.FullName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox and button on my form. The combobox has categories in
I have wpf UserControl containing a combobox and a textbox in a row. Currently
I have a Crystal Report I'm trying to recreate from scratch after an update
I have two ComboBoxes in a WinForm application that display COM ports detected on
I have been trying to use the query functionality of the ItemFileReadStore to filter
I have a panel with AutoScroll set to true . In it, I am
I am trying to style a WPF ComboBox so that it matches a black-theme
I have just downloaded Obout.com suite controls for ASP.NET 3.5 and 4.0. I made
I've been trying to make a ComboBox which would suggest options as you type,
I would like to use a combobox as the itemEditor for one of the

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.