Solved

Reading Specific Line In Text File - C# App

Posted on 2006-10-30
7
3,911 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

20 Experts available now in Live!

Get 1:1 Help Now