-2

I'm new to C# obviously.

I'm looking for a way to read each line in a text file and search for a unique string in that text file. If if finds the string, then I need it to read the next line only and output it to a textbox.

Any help would be appreciated.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
TheInfamousOne
  • 175
  • 1
  • 4
  • 17
  • Welcome to Stack Overflow! What have you tried so far? – Nate Barbettini Feb 15 '15 at 21:09
  • You already have the correct tags, have you tried searching on google for exactly that? 'c# streamreader'? it should give you something to go on.. – Sayse Feb 15 '15 at 21:09
  • Read this https://msdn.microsoft.com/en-us/library/aa287535%28v=vs.71%29.aspx and tell us if you don't understand anything. – Nadia Chibrikova Feb 15 '15 at 21:12
  • @Sayse what's so bad about searching on Stackoverflow? This place is for questions and answers is it not? – reformed Feb 15 '15 at 21:17
  • @reformed - Thats just it, there hasn't been any searching, if there had been I'm sure the OP would have found [this question](http://stackoverflow.com/questions/6510788/reading-the-next-line-of-a-file-only-once) which has [this answer](http://stackoverflow.com/a/6510835/1324033) which is almost an exact duplicate of the one posted here. StackOverflow is a place for questions and answers yes, but it is expected that those questions show a level of research effort – Sayse Feb 15 '15 at 21:25
  • @Sayse and how do you know that? He's obviously new to Stackoverflow and to C# as he stated, what if he's clueless about `how` to search? Some of you SO folks are ruthless. – reformed Feb 15 '15 at 21:28
  • @reformed One of the things mentioned in the [tour](http://stackoverflow.com/tour) is " Include details about what you have tried and exactly what you are trying to do." - No details of what has been tried are present. and even then answers would depend entirely on what framework as to what valid properties and methods the "textbox" has. As I said previously, the first thing that appears when typing C# streamreader into google is the msdn page that even includes an example that would start the OP off – Sayse Feb 15 '15 at 21:36

2 Answers2

1

you can enumerate through lines until you find the unique string and set the next line as text box value and break the operation

bool found = false;
foreach (var line in File.ReadLines("filepath"))
{
    if (found)
    {
        textBox1.Text = line;
        break;
    }
    found = line.Contains("unique string");                
}
textBox1.Text = "not found";

File.ReadLines(file) read the lines in specified file one by one.

foreach(var item in container) will get items from its container one by one and you can do your stuff with item.

y.Contains(x) checks whether y contains x or not.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • 1
    `else if (found)` (or better still put the second if first) – Nadia Chibrikova Feb 15 '15 at 21:14
  • This did not exactly work as planned. It's great that I found the uniquestring I was looking for and can return it. But I wanted to output the next line in the text file, not the unique string. For example. The text file will have the product name in line 1. And the product version in line 2. I want to find the product name in line one and return the product version in line 2 to the textbox. Any ideas? – TheInfamousOne Feb 15 '15 at 22:54
-2

Similar to the other answer, but this involves the StreamReader class:

using (StreamReader r = new StreamReader("filename.ext"))
{
    string line, version = "";
    bool nameFound = false;
    while ((line = r.ReadLine()) != null)
    {
        if (nameFound)
        {
            version = line;
            break;
        }

        if (line.IndexOf("UniqueString") != -1)
        {
            nameFound = true;
            // current line has the name
            // the next line will have the version
        }
    }

    if (version != "")
    {
        // version variable contains the product version
    }
    else
    {
        // not found
    }
}
reformed
  • 4,505
  • 11
  • 62
  • 88
  • Care to elaborate on the downvote? – reformed Feb 15 '15 at 21:44
  • This works fine, but I'm trying to get the next line in the text file. Example: So if my text file looks like this. line 1: "uniquestring" line 2: text I want to send to my textbox. the text file contains a bunch of items line by line. for instance line1: name of product line2: version of product line3: name of product line4: version of product. So, if I look for the name, I like to send the version of product to the textbox, not the name of product. – TheInfamousOne Feb 15 '15 at 22:11
  • @TheInfamousOne In a `while` loop, if you were to find the unique string in the current iteration, and if your input file's format guarantees that the next line is the version, then the immediate next iteration would include the corresponding version you are looking for. See edit. – reformed Feb 15 '15 at 22:28
  • Thanks, this worked perfect. I'm new to C# and could not get this to work at first, but once I started stepping through the debug process, I saw that I forgot to change my variable name to "line". Once that was fixed, I could see the "next line" was read. I'm see what is going on, but I'm trying to understand the code. Thanks again, I'll learn it eventually. – TheInfamousOne Feb 16 '15 at 00:02