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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:21:49+00:00 2026-05-23T02:21:49+00:00

I have to open and read from a .txt file, here is the code

  • 0

I have to open and read from a .txt file, here is the code I’m using:

Stream myStream;
openFileDialog1.FileName = string.Empty; 
openFileDialog1.InitialDirectory = "F:\\";
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{
    var compareType = StringComparison.InvariantCultureIgnoreCase;
    var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
    var extension = Path.GetExtension(openFileDialog1.FileName);
    if (extension.Equals(".txt", compareType))
    {
        try 
        { 
            using (myStream = openFileDialog1.OpenFile()) 
            { 
                string file = Path.GetFileName(openFileDialog1.FileName);
                string path = Path.GetFullPath(file); //when i did it like this it's work fine but all the time give me same path whatever where my "*.txt" file is
                //Insert code to read the stream here. 
                //fileName = openFileDialog1.FileName; 
                StreamReader reader = new StreamReader(path);
                MessageBox.Show(file, "fileName");
                MessageBox.Show(path, "Directory");
            } 
        } 
        // Exception thrown: Empty path name is not legal
        catch (ArgumentException ex) 
        { 
            MessageBox.Show("Error: Could not read file from disk. " +
                            "Original error: " + ex.Message); 
        } 
    }
    else 
    {
        MessageBox.Show("Invaild File Type Selected");
    } 
} 

The code above throws an exception which says “Empty path name is not legal”.

What am I doing wrong?

  • 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-05-23T02:21:50+00:00Added an answer on May 23, 2026 at 2:21 am

    As pointed by hmemcpy, your problem is in the following lines

    using (myStream = openFileDialog1.OpenFile())
    {
       string file = Path.GetFileName(openFileDialog1.FileName);
       string path = Path.GetDirectoryName(file);
       StreamReader reader = new StreamReader(path);
       MessageBox.Show(file, "fileName");
       MessageBox.Show(path, "Directory");
    } 
    

    I’m going to break down for you:

    /*
     * Opend the file selected by the user (for instance, 'C:\user\someFile.txt'), 
     * creating a FileStream
     */
    using (myStream = openFileDialog1.OpenFile())
    {
       /*
        * Gets the name of the the selected by the user: 'someFile.txt'
        */
       string file = Path.GetFileName(openFileDialog1.FileName);
    
       /*
        * Gets the path of the above file: ''
        *
        * That's because the above line gets the name of the file without any path.
        * If there is no path, there is nothing for the line below to return
        */
       string path = Path.GetDirectoryName(file);
    
       /*
        * Try to open a reader for the above bar: Exception!
        */
       StreamReader reader = new StreamReader(path);
    
       MessageBox.Show(file, "fileName");
       MessageBox.Show(path, "Directory");
    } 
    

    What you should do is to cahnge the code to something like

    using (myStream = openFileDialog1.OpenFile())
    {
       // ...
       var reader = new StreamReader(myStream);
       // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that downloads a file from the internet located here: http://www.amsat.org/amsat/ftp/keps/current/nasa.all
I have a FAT12 image file and I have to open it and read
I have the following code: f = open(path, 'r') html = f.read() # no
I have a open file dialog with three filters: QString fileName = QFileDialog::getOpenFileName( this,
Hi all i have written a small code to open a particular text file.
I have a perl code to compare 2 strings First string I get from
I have read a lot of popular standards manuals for open source PHP projects.
I have this command in a Rails controller open(source) { |s| content = s.read
In php I have open a .php file and want to evaluate certain lines.
I have to open a page from class extended from javax.swing.AbstractAction class... Is that

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.