-
April 3rd, 2011, 03:37 PM
#1
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.
-
April 5th, 2011, 10:42 PM
#2
Re: Problem with StreamReader off of a button
My first thought is to check the lines
if (print == true)
txtResult.Text = line;
}//End while statement
I think it's reading all the lines but each new line overwrites the preceding line. See the "txtResult.Text = line" statement ? You aren't adding a new line or lengthening the existing line, I think you're overwriting the existing line.
That'd be the first place I'd look for a problem
Do you use the debugger ? If so, set a breakpoint there. If not, START !
incidentally, there seems to be a coupla other problems as well, but it's now running on my 'chine.
best wishes.
OldFool
Last edited by ThermoSight; April 5th, 2011 at 11:00 PM.
-
April 6th, 2011, 01:11 AM
#3
Re: Problem with StreamReader off of a button
Yes, you are replacing the txtResult instead of appending to it. Try
Code:
txtResult.Text += line;
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|