Solved

Reading Specific Line In Text File - C# App

Posted on 2006-10-30
7
3,927 Views
Last Modified: 2012-08-14
Hey everyone,

I am trying to create a program that will read a file, extract two lines and then act based on what those lines say.  

What I need help with is actually reading the two lines.  I am using the following code to read the first line which is www.yahoo.com but I am not sure how to read line number two, and write it to a text box.

            System.IO.StreamReader StreamReader3 =
            new System.IO.StreamReader("website.txt");
            txtWebsite.Text = StreamReader3.ReadToEnd();
            StreamReader3.Close();

What code can I get to read only line two which says "Yahoo!!!" into a text box called txtSiteName.Text
0
Comment
Question by:angus_young_acdc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 17833163
you can use regular expressoin to read the specified word
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 17833330
string line;

 StreamReader reader = new StreamReader("website.txt");
 while((line = reader.ReadLine()) != null)
{
      if(line.IndexOf("Yahoo") != -1)
      {
            // Do something
      }
}
reader.Close();

Dorothy
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17833492
Hi Dorothy,

I have tried using your code but a little stumped.    I am unsure how to get it to read a specific line, paste that to the correct text box, and then do the same for the 2nd line.   This is the theory (using your code) of what I want it to do:

          string line;
           
            StreamReader reader = new StreamReader("website.txt");
            while ((line = reader.ReadLine()) != null)
            {
                if (line.IndexOf("Yahoo!!!") != -1)
                {
                    // read line one, put contents of line one into txtWebsite.Text
                    // read line two, out contents of line two into txtSiteName.Text
                }
            }
            reader.Close();


When I tried having just one line read and put into txtWebsite.Text I get this error:

I get the following error:
Error      2      Cannot convert method group 'ReadToEnd' to non-delegate type 'string'. Did you intend to invoke the method?      C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\EasyAxs\EasyAxs\frmMainSystem.cs      178      37      EasyAxs

using this code:
            string line;
           
            StreamReader reader = new StreamReader("website.txt");
            while ((line = reader.ReadLine()) != null)
            {
                if (line.IndexOf("Yahoo!!!") != -1)
                {
                    txtWebsite.Text = reader.ReadToEnd;
                }
            }
            reader.Close();
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 6

Accepted Solution

by:
dorothy2 earned 30 total points
ID: 17833574
To test, I wrote a console app rather than a Windows app. Just substitute populating your text box where I am writing the lines to the console. The code below reads the lines like my previous example. There is a flag to indicate when you find "Yahoo"". Then code loops one more time, but the second condition, foundIt == false, is no longer met (it's true now) so you break out of the loop. Line is already populated:

                        string line;
                  bool foundIt = false;
                  StreamReader reader = new StreamReader("C:\\Temp\\Yahoo.txt");
                  while((line = reader.ReadLine()) != null && !foundIt)
                  {
                        if(line.IndexOf("Yahoo") != -1)
                        {
                              Console.WriteLine("This is the first line:");
                              Console.WriteLine(line);
                              foundIt = true;
                              
                        }
                  }

                  if(foundIt)
                  {                        
                        Console.WriteLine("This is the second line:");
                        Console.WriteLine(line);
                  }
                  reader.Close();            
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17833684
Excellant thanks dorothy that worked brilliantly :)  

Last question on it (obviously I will increase the points).  To save multiple lines I thought it would be something like this:
                System.IO.StreamWriter StreamWriter1 =
                new System.IO.StreamWriter("alarm.txt");
                StreamWriter1.Write(txtWebsite.Text);
                StreamWriter1.NewLine;
                StreamWriteer1.Write(txtSiteName.Text);
                StreamWriter1.Close();

But that just brings up the following error:
Error      2      Only assignment, call, increment, decrement, and new object expressions can be used as a statement      C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\EasyAxs\EasyAxs\frmMainSystem.cs      221      17      EasyAxs


How can I then get it to write the 2nd textbox (txtSiteName) to the 2nd line?  So that it will look like this in the file:
www.yahoo.com
Yahoo!!!
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17833696
Can't see an edit button but new System.IO.StreamWriter("alarm.txt") should be "website.txt"
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17833702
Never mind I got it sorted myself :)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding items to a C# list incrementally 5 64
Securing WEBAPI on Azure 2 28
Return array 3 20
c#, datatable, aspx 4 25
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question