Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading Specific Line In Text File - C# App

Posted on 2006-10-30
7
Medium Priority
?
3,967 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
dorothy2 earned 120 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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