Problem with StreamReader off of a button
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Problem with StreamReader off of a button

Threaded View

  1. #1
    Join Date
    Apr 2011
    Posts
    0

    Problem with StreamReader off of a button

    Hello,

    I am trying to make a program off of a Windows Forms Application. I am using Microsoft Visual Studio 2008 3.5 framework. The language is C#. The purpose of this program is for the user to browse through a file dialog to find a text file off of a button. When the user clicks the button, the directory is stored in a text box. After this, there's another button where the user will click Run. When this happens, a Stream Reader will read through the selected file inside of the text box. The Output/result will display in a multiline text box below.

    Here's the code:

    Code:
    using System;
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    
    using System.Windows.Forms;
    
    namespace TI
    {
        public partial class TIGUI : Form
        {
            //Form that shows a browse feature and a run button
    
            //default contructor
            public TlGUI()
            {
    
    
             InitializeComponent();
    
            } //end constructor
    
            //handles click event of btnBrowse_Click
            private void btnBrowse_Click(object sender, EventArgs e)
            {
                
                    //When clicked, method opens up a browse feature
                    //Provided by VS
                    OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
                    //  OpenFileDialog1.InitialDirectory = @"C:\";
                    OpenFileDialog1.Filter = "Text|*.txt|All|*.*";
                    if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
    
                        txtString.Text = OpenFileDialog1.FileName;
                    btnBrowse.Enabled = true;
            } //end method btnBrowse_Click
    
    
            //handles click event of btnRun_click
            private void btnRun_Click(object sender, EventArgs e)
            {
                string line = "";
                bool print = false;
                using (StreamReader reader = new StreamReader(txtString.Text))
                {
                    while (reader.EndOfStream)
                    { 
                        // Read line and place into string..we will use line in our following IF's
                        line = reader.ReadLine();
                        if (line == "==========  GENERAL INFO  ==========")
                            print = false;
                        // If line == Server Usage Header then stop printing
                        if (line == "==========  SERVER USAGE  ==========")
                            print = true;
    
                        if (print == true)
                            txtResult.Text = line; 
    
                    }//End while statement
                }//End using StreamReader
            }//End of Method btnRun_Click
        }//End of Class
    }//End of Namespace
    ---
    This is the dummy text file I am using:
    Code:
     
    ==========  GENERAL INFO  ==========
    GENERATED_ON=
    VERSION=
    SYSTEM_ID=
    MODEL_NO=
    HOSTNAME=
    LOCATION=
    ADMIN_EMAIL=
    UPTIME= 
    
    ==========  SERVER USAGE  ==========
    Resource            Size GiB  Used GiB  Avail GiB  Use%  Cleanable GiB*
    ------------------  --------  --------  ---------  ----  --------------
    /backup: pre-comp          -    3205.3          -      -                -
    /backup: post-comp    2549.2      371.2      2178.0    15%              0.0
    /ddvar                  19.7        3.6        15.1    19%                -
    ------------------  --------  --------  ---------  ----  --------------
    * Estimated based on last cleaning of 2011/02/15 06:37:04.
    ---

    the bold area is where i think the issue is. I receive no errors, but when i debug the program, it only reads the last line.

    if anyone has any questions or can give suggestions, please ask and I'll be happy to cooperate.

    Thank you!
    Last edited by HanneSThEGreaT; April 6th, 2011 at 09:31 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center