Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to Select a *.txt or *.doc file from the system using FileDialog Control in C#?
and Can anyone plz tell How to Display the text of the file in the richtextbox control in the form?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 2:15am    
What, broken MSDN?
--SA
Member 12068429 19-Nov-15 15:40pm    
what about with png?

Hi aditya,

C#
OpenFileDialog dialog = new OpenFileDialog();
           dialog.Filter =
              "txt files (*.txt)|*.txt|All files (*.*)|*.*";
           dialog.InitialDirectory = "C:\\";
           dialog.Title = "Select a text file";
           if (dialog.ShowDialog() == DialogResult.OK)
           {
               string fname = dialog.FileName;
                         richTextBox1.Text= System.IO.File.ReadAllText (fname);

           }



try this code
 
Share this answer
 
You can take use of Filters to show only DOC and TXT files

C#
OpenFileDialog dialog1 = new OpenFileDialog();
dialog1.Filter = "txt files (*.txt)|*.txt| DOC files (*.doc)|*.doc";


you can load files in Richtextbox, by using LoadFile method

//for DOC File
richTextBox1.LoadFile(FilePath, RichTextBoxStreamType.RichText); 

//for TXT File, just change streamtype to plain text
richTextBox1.LoadFile(FilePath,  RichTextBoxStreamType.PlainText); 
 
Share this answer
 
try this

dialog was openDialog ID
C#
dialog.Filter = "txt files |*.txt|Word Documents|*.doc";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900