Solved

Reading Specific Line In Text File - C# App

Posted on 2006-10-30
7
3,923 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 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