Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I m doing program on C# windows base where I code for getting the path for a file in text box but when I try to open the file file in another text box it is not opening
below I give the code
private void btnShow_Click(object sender, EventArgs e)
       {
           //001: Check the user selected a file and pressed ok button. Before opening the dialog
           //set the filters
           dlgFileOpen.Filter = "Bitmap Files(*.bmp)|*.bmp|Other Image Files(*.jpg)|*.jpg|" +
           "Text Files(*.txt) | *.txt|All Files(*.*)|*.*";
           if (dlgFileOpen.ShowDialog() == DialogResult.OK)
           {
               txtSelectedFile.Text = dlgFileOpen.FileName;
               string ext = Path.GetExtension(dlgFileOpen.FileName);

           if (ext != ".txt")
           {
               //002: If the extension is Bmp or Jpg display it in the Picture box
               txtFileContent.Visible = false;
               //txtFileContent.Visible = false;
               pict.Visible = true;
               pict.SizeMode = PictureBoxSizeMode.StretchImage;
               pict.Load(dlgFileOpen.FileName);
           }
           else
           {
               //003: The extension is txt. Read the file and display the content
               txtFileContent.Visible = true;
               pict.Visible = false;
               FileStream fstream = new FileStream(dlgFileOpen.FileName, FileMode.Open);
               StreamReader reader = new StreamReader(fstream);
               while (reader.EndOfStream != true)
               {

                    //here it is reading but not open in the text box [txtFileContent]
                   txtFileContent.AppendText(reader.ReadLine());
                   txtFileContent.AppendText(Environment.NewLine);
               }
               reader.Close();
           }
           }
         }
Posted
Updated 6-Jul-11 1:22am
v3
Comments
Timberbird 6-Jul-11 7:31am    
What do you see in the debugger, are lines actually being read? In your last while, read lines to variable and put a breakpoint there to check
StM0n 6-Jul-11 7:31am    
So... what does it do instead? Just blank or something else?
UJimbo 6-Jul-11 7:37am    
I just tested your code and it reads and appends the contents of a text file to a textbox perfectly. Make sure dlgFileOpen.FileName returns the proper path to the text file you're trying to read is my only suggestion

Try this
C#
private void button1_Click(object sender, EventArgs e)
        {
            //001: Check the user selected a file and pressed ok button. Before opening the dialog
            //set the filters
            dlgFileOpen.Filter = "Bitmap Files(*.bmp)|*.bmp|Other Image Files(*.jpg)|*.jpg|" +
            "Text Files(*.txt) | *.txt|All Files(*.*)|*.*";
            if (dlgFileOpen.ShowDialog() == DialogResult.OK)
            {
                txtSelectedFile.Text = dlgFileOpen.FileName;
                string ext = Path.GetExtension(dlgFileOpen.FileName);
//Changes are done in below line
                if (ext.ToLower() == ".bmp" || ext.ToLower() == ".jpg")
                {
                    //002: If the extension is Bmp or Jpg display it in the Picture box
                    txtFileContent.Visible = false;
                    //txtFileContent.Visible = false;
                    pict.Visible = true;
                    pict.SizeMode = PictureBoxSizeMode.StretchImage;
                    pict.Load(dlgFileOpen.FileName);
                }
                else
                {
                    //003: The extension is txt. Read the file and display the content
                    txtFileContent.Visible = true;
                    pict.Visible = false;
                    FileStream fstream = new FileStream(dlgFileOpen.FileName, FileMode.Open);
                    StreamReader reader = new StreamReader(fstream);
                    while (reader.EndOfStream != true)
                    {

                        //here it is reading but not open in the text box [txtFileContent]
                        txtFileContent.AppendText(reader.ReadLine());
                        txtFileContent.AppendText(Environment.NewLine);
                    }
                    reader.Close();
                }
            }
        }
 
Share this answer
 
v3
Replace your else block with this:

C#
txtFileContent.Visible = true;
pict.Visible = false;
TextReader reader = new StreamReader(dlgFileName.FileName);
txtFileContent.Text = reader.ReadToEnd();
reader.Close();
 
Share this answer
 
v3
Comments
[no name] 6-Jul-11 8:00am    
i think OP is saying that when he try to open file other than text file the code throws an exception.

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