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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:03:54+00:00 2026-06-04T08:03:54+00:00

What I am trying to do is to read in a file to a

  • 0

What I am trying to do is to read in a file to a richTextBox automatically with the OnSelectedIndexChange method. There arent any errors, it just flat out doesnt work. Heres the code that I am working with

    public void comboBox1_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedPath = comboBox1.SelectedItem.ToString();
        if (File.Exists(@"C:\\Mavro\\MavBridge\\" + selectedPath + "\\ " + "Comment" + ".txt"))
        {
            try
            {
                Thread.Sleep(0500);
                System.IO.StreamReader textFile = new System.IO.StreamReader(@"C:\\Mavro\\MavBridge\\" + selectedPath + "\\ " + "Comment" + ".txt");
                richTextBox1.Text = textFile.ReadToEnd();
                textFile.Close();
            }
            catch
            {                
                MessageBox.Show("Error: File cannot be opened!", "Error");
            }
        }
        else
        {
            MessageBox.Show("No comment was found in this folder", "Alert");
        }
    }
  • 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-04T08:03:55+00:00Added an answer on June 4, 2026 at 8:03 am

    Just for fun, lets have you try something. First, replace the following line:

    if (File.Exists(@"C:\\Mavro\\MavBridge\\" + selectedPath + "\\ " + "Comment" + ".txt"))
    

    with this:

    if(File.Exists(string.Format("C:\\Mavro\\MavBridge\\{0}\\Comment.txt", selectedPath)))
    

    It looks like you had an extra space ("\\ " + "Comment"), so I’m sure that’s why it never hits this block of code. Also, anytime you have an object that needs to be closed/disposed, more often than not it implements IDisposable, meaning you should encapsulate the object within a using block:

    Thread.Sleep(0500);
    try
    {
        using(System.IO.StreamReader textFile = new System.IO.StreamReader(string.Format("C:\\Mavro\\MavBridge\\{0}\\Comment.txt", selectedPath)))
        {
            richTextBox1.Text = textFile.ReadToEnd();
        }
    
    }
    catch
    {                
        MessageBox.Show("Error: File cannot be opened!", "Error");
    }
    

    However, this can be simplified even further by bypassing the StreamReader entirely and using System.IO.File.ReadAllText instead:

    richTextBox1.Text = System.IO.File.ReadAllText(string.Format("C:\\Mavro\\MavBridge\\{0}\\Comment.txt", selectedPath));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read the following file (The comments are not there in
I am trying to read file headers using java, I want to get file
I am trying to read file and split its line to get some context(Computer
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using
I'm trying to read a file line by line starting from a specific line
I am trying to read a file(temp.txt) placed under /root directory on remote host(ubuntu)
I am trying to read a file and then display the file in ASCII
I am trying to read from file: The file is multiline and basically i
I am trying to read a file and the error i get is java.io.FileNotFoundException:
I'm trying to read a file of regexes, looping over them and filtering them

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.