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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:21+00:00 2026-06-17T13:59:21+00:00

My .NET Windows application has multiple instances of being able to open and view

  • 0

My .NET Windows application has multiple instances of being able to open and view text files. I have no problem getting this to work when I create individual openFileDialogs through my code. In trying to clean up my code, I created a method, but I am fairly new and am getting a red squiggly. Can someone please look at this and tell me what I am doing wrong? Thanks.

I am trying to call the method as shown here:

textBox_ViewFile.Text = LoadFromFile();

Then my method is as follows:

static string[] LoadFromFile()
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.RestoreDirectory = false;
        openFileDialog1.ShowHelp = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if (openFileDialog1.OpenFile() != null)
                {
                    return (File.ReadAllLines(openFileDialog1.FileName));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

In this case, I am getting a syntax error in VS2010 under “LoadFromFile()” from

static string[] LoadFromFile()"
  • 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-17T13:59:22+00:00Added an answer on June 17, 2026 at 1:59 pm

    You’re missing a return statement if either of your if statements are false

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if (openFileDialog1.OpenFile() != null)
                {
                    return (File.ReadAllLines(openFileDialog1.FileName));
                }
                // missing
                return null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                // missing
                return null;
            }
        }
        // missing
        return null;
    

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

Sidebar

Related Questions

Im using c# .net windows form application. i have a datagrid view. It has
I have a .NET Windows application in the production that has no access to
I have one .net windows application I'm trying to read .xml file from c#
Im using c# .net windows application form. I have created many databases with many
I have a .NET windows forms application compiled as x86 – it needs to
Im using c# .net windows form application. i have a SQL database with 5
Im using c# .net windows form application. I have many databases created using sql
Im using c# .net windows form application. I have loaded names of all the
I have windows forms application with multiple forms and controls in them. I want
I have a basic ASP.Net MVC 3 application which has a number of controllers

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.