Solved

Reading Specific Line In Text File - C# App

Posted on 2006-10-30
7
3,914 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now