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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:05:54+00:00 2026-05-15T20:05:54+00:00

Hi all i have written a small code to open a particular text file.

  • 0

Hi all i have written a small code to open a particular text file. Now on open file dialog under the file name a drop down is displaying some file names if i select a file from that and click on ok i would like to display an error message as wrong file selected.

My code to open particular text file is as follows

openFileDialog1.FileName = string.Empty;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
openFileDialog1.InitialDirectory = strFilePath;
openFileDialog1.Filter = "(*.txt)|FileHeader*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
                //FileName = openFileDialog1.FileName;
                txtFileHeader.Text = openFileDialog1.FileName.ToString(); ;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    }

}

If any modifications to be done please let me know.


I just write my code as follows as i did not find any declaration like var

            string compareType = StringComparison.InvariantCultureIgnoreCase;
            string fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
            string extension = Path.GetExtension(openFileDialog1.FileName);
            if (fileName.StartsWith("FileHeader", compareType)
                && extension.Equals(".txt", compareType))

I am Getting the errors as

Cannot implicitly convert type ‘System.StringComparison’ to ‘string’

Error 3 The best overloaded method match for ‘string.StartsWith(string, System.StringComparison)’ has some invalid arguments

Error 4 Argument ‘2’: cannot convert from ‘string’ to ‘System.StringComparison’

Error 5 Static member ‘string.Equals(string, string)’ cannot be accessed with an instance reference; qualify it with a type name instead

So can any one please what to do…

  • 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-15T20:05:54+00:00Added an answer on May 15, 2026 at 8:05 pm

    How about something like this? You could also use regex, but I don’t have VS on this machine and felt like being lazy.

    I’d add, while you could file off the incorrect file name. It may be better to add an All Files (*.*)|*.* to file filter list and allow the user to select anything he might like. Then allow the application to fail during the file read if they select the wrong file. This would be more of an expected use case that would match how most other applications function.

    Stream myStream;
    openFileDialog1.FileName = string.Empty; 
    openFileDialog1.InitialDirectory = @"..\..\ACH"; 
    openFileDialog1.Filter = "FileHeader (FileHeader*.txt)|FileHeader*.txt"; 
    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
        var compareType = StringComparison.InvariantCultureIgnoreCase;
        var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
        var extension = Path.GetExtension(openFileDialog1.FileName);
        if (fileName.StartsWith("FileHeader", compareType)
            && extension.Equals(".txt", compareType))
        {
            try 
            { 
                using (myStream = openFileDialog1.OpenFile()) 
                { 
                    // Insert code to read the stream here. 
                    //FileName = openFileDialog1.FileName; 
                    txtFileHeader.Text = openFileDialog1.FileName;
                } 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("Error: Could not read file from disk. " +
                                "Original error: " + ex.Message); 
            } 
        }
        else 
        {
            MessageBox.Show("Invaild File Type Selected");
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.