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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:32:41+00:00 2026-06-12T20:32:41+00:00

I am sorry for the weird title. I am working on a WPF app

  • 0

I am sorry for the weird title. I am working on a WPF app where I am supposed to load a text file from the system by clicking a button and read the contents of the file and display the content of textfile in a textbox.

I have done the following:

XAMl:

<TextBox Grid.Column="2" Text="{Binding Path=WriteMessage, Mode=TwoWay}" Name="MessageWrite" />
<Button Content="Load" Command="{Binding Path=LoadFileCommand}" Name="button8" />

ViewModel Class:

    // Method gets called when LOAD Button is Clicked
    private void ExecuteLoadFileDialog()
    {
        FileReader mFile = new FileReader(); // Its a Class Which Reads The File

        var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
        dialog.ShowDialog();
        dialog.DefaultExt = ".txt";
        dialog.Filter = "Text Files(*.txt)|*.txt|All(*.*)|*";
        string path;
        path = dialog.FileName;

        using (StreamReader sr = new StreamReader(path)) 
        {
            WriteMessage = sr.ReadToEnd();
        }            
    }

File Reader Class:

class FileReader : I2CViewModel.IFileReader
{
    public string Read(string filePath)
    {
        byte[] fileBytes = File.ReadAllBytes(filePath);
        StringBuilder sb = new StringBuilder();
        foreach (byte b in fileBytes)
        {
            sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
        }
        return sb.ToString();
    }
}

The problem I am facing here is, when I click the load Button, it opens a filedialog but it displays all the files rather than just displaying .Txt Files. How to make sure only .txt files are visible?

Secondly when I click the button, the dialog pops up and if i click cancel button, the application crashes showing “Empty path name is not legal.” which is pointing to using (StreamReader sr = new StreamReader(path))

How can I clear these issues?? 🙂

  • 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-12T20:32:42+00:00Added an answer on June 12, 2026 at 8:32 pm

    Two quick things:

    You are setting the filter AFTER showing the dialog thus rendering the filter useless

    You should wrap the using block on an if to see if the user canceled the operation (dialog’s FileName property stays empty) and wrap the readToEnd call on a try – catch block just in case

    Those are my quick 2 cents

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

Sidebar

Related Questions

Sorry, weird title. Anyway, I add a div and some text to a Control
Sorry about the weird title, I'm currently playing around with WinForms and I'm wondering
I am working on a textbox and combobox in my wpf app. I am
Well folks I am sorry for the weird title but couldnt find a better
I am a C++ developer and recently started working on WPF. I am sorry
I am sorry for the weird title but I coulnt figure out how to
Sorry about the weird question title, but I don't really know what to call
Sorry for the weird title, but well, that's exactly what I need. I know
Sorry for the really weird title, but here’s what I’m trying to do: var
I have a weird encoding problem from my PyQt app to my mysql database.

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.