2 Replies - 6250 Views - Last Post: 16 June 2011 - 12:47 PM Rate Topic: -----

#1 Crash95   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 40
  • Joined: 08-April 11

Opening file dialog and using streamreader

Posted 16 June 2011 - 12:23 PM

Hi all. I have a problem. I started to play a little with streamreader, and i know how to put some exact pointed file to the textbox. My problem is how can i use the open file dialog so that the program can do this: He clicks Open File button and the dialog will show letting him to choose which file can he read (allowed just .txt and .docx files) and when he chooses a file the text from that file would be written in the textbox i have tryed something like this but i dont know how i can put into streamreader the file which he chooses.
OpenFileDialog data = new OpenFileDialog();
            data.Title = "Izaberite fajl";
            data.InitialDirectory = @"C:\Users\ogatalo\Desktop";
            data.Filter = "TXT files (*.txt*)|*.docx";
            data.FilterIndex = 1;
            data.RestoreDirectory = true;
//problem starts here 
            StreamReader sr = new StreamReader(@"C:\\Users\ogatalo\Desktop\top15 skripta.txt"); 
            string txt = sr.ReadToEnd();
            richTextBox1.Text = txt;
            sr.Close();


Is This A Good Question/Topic? 0
  • +

Replies To: Opening file dialog and using streamreader

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Opening file dialog and using streamreader

Posted 16 June 2011 - 12:37 PM

Well, first of all, you do understand that a StreamReader is going to get nothing but garbage from a .docx file? Docx isn't text. It's is actually a compressed (zipped) file that contains a bunch of xml and manifest files.

Other than that, you've got a few things wrong. Your filter, for one. Also, you have to show the dialog, not just create it. Then you have to check that the user actually selected a file. And last, you need to make absolutely sure to close your streams. I prefer to use a using block, which will call the .Dispose method on the stream, which takes care of flushing and closing the buffer.

Here's some sample code:

OpenFileDialog ofd = new OpenFileDialog() {
    Title = "Choose a File",
    InitialDirectory = @"c:\dev\",
    Filter = "Text Files (.txt)|*.txt|XML Files|*.xml|Word Documents (.docx)|*.docx",
    RestoreDirectory = true,
    Multiselect = false
};
if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) {
    MessageBox.Show("No file selected!");
    return;
}
using (StreamReader sr = new StreamReader(ofd.FileName)) {
    textBox1.Text = sr.ReadToEnd();
}

Was This Post Helpful? 1
  • +
  • -

#3 Crash95   User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 40
  • Joined: 08-April 11

Re: Opening file dialog and using streamreader

Posted 16 June 2011 - 12:47 PM

Thank you very much. Now I understand how it works :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1