none
reading specific line on a txt file from the web RRS feed

  • Question

  • Hi

    I'm trying to read multiple lines in a text file from the web,
    WebClient web1 = new WebClient();
    System.IO.Stream stream1 = web1.OpenRead("txt file url");
    using (System.IO.StreamReader reader1 = new System.IO.StreamReader(stream1))
    {
            String result = reader1.ReadToEnd();
            string enabled = result.ElementAt(0).ToString();
            string url = result.ElementAt(1).ToString();
    
            if (enabled == "false")
                  { return; }
            else if (enabled == "true")
                  {
                     Process.Start(url);
                  }
             }

    Now I'm not really sure about the "result.ElementAt", this gives a char only.. and I need the full line..

    the txt file template:


    Any help please?
    Thanks in advance.
    Sunday, February 4, 2018 1:47 AM

Answers

  • The following is one way to do it. Note that this is just using a StreamReader from disk, I assume you can modify this to read from the web.

    using (System.IO.StreamReader reader1 = new System.IO.StreamReader(filename))
    {
        // get the first line
        string result = reader1.ReadLine();
        // get the second line
        string url = reader1.ReadLine();
        // check result to determine if it is not false (true)
        if (!result.ToLower() != "false")
        {
            Console.WriteLine("Not enabled");
            return;
        }
        // if it is not not enabled then it is enabled
        Console.WriteLine("Navigate to " + url);
    }
    



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Zuher Laith Monday, February 5, 2018 7:47 AM
    Sunday, February 4, 2018 3:12 AM

All replies

  • The following is one way to do it. Note that this is just using a StreamReader from disk, I assume you can modify this to read from the web.

    using (System.IO.StreamReader reader1 = new System.IO.StreamReader(filename))
    {
        // get the first line
        string result = reader1.ReadLine();
        // get the second line
        string url = reader1.ReadLine();
        // check result to determine if it is not false (true)
        if (!result.ToLower() != "false")
        {
            Console.WriteLine("Not enabled");
            return;
        }
        // if it is not not enabled then it is enabled
        Console.WriteLine("Navigate to " + url);
    }
    



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Zuher Laith Monday, February 5, 2018 7:47 AM
    Sunday, February 4, 2018 3:12 AM
  • Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 2:

    string line = File.ReadLines(FileName).Skip(1).Take(1).First();

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (kokkisajee) or Facebook (sajeetharan) profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers


    Sunday, February 4, 2018 3:22 AM
  • The following is one way to do it. Note that this is just using a StreamReader from disk, I assume you can modify this to read from the web.

    using (System.IO.StreamReader reader1 = new System.IO.StreamReader(filename))
    {
        // get the first line
        string result = reader1.ReadLine();
        // get the second line
        string url = reader1.ReadLine();
        // check result to determine if it is not false (true)
        if (!result.ToLower() != "false")
        {
            Console.WriteLine("Not enabled");
            return;
        }
        // if it is not not enabled then it is enabled
        Console.WriteLine("Navigate to " + url);
    }
    Simple Samples

    How is "reader1" logically determined to read the second line ?
    although it works, don't mind my curiosity.

    I didn't use (!result.ToLower() != "false") method,
    I used the same method I have and it worked: "
    string enabled = reader1.ReadLine();   
    string url = reader1.ReadLine();
    
    MessageBox.Show(enabled.ToString() + "\n" + url.ToString());
    
    if (enabled == "false")
        { return; }
    else if (enabled == "true")
    {
        Process.Start(url);
    }

    So is it safe If I don't use (ToLower) ?
    or would be recommended to use it in any case?
    and what's the difference?

    thank you so much!
    • Edited by Zuher Laith Sunday, February 4, 2018 12:21 PM
    Sunday, February 4, 2018 12:00 PM
  • Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 2:

    string line = File.ReadLines(FileName).Skip(1).Take(1).First();
    Sajeetharan s

    Correct, I am familiar with this method,
    but didn't know how to make it work with WebClient.
    • Edited by Zuher Laith Sunday, February 4, 2018 12:20 PM
    Sunday, February 4, 2018 12:04 PM
  • Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 2:

    string line = File.ReadLines(FileName).Skip(1).Take(1).First();

    Sajeetharan s

    Correct, I am familiar with this method,
    but didn't know how to make it work with WebClient.
    You have not explained why you need that. As best as I understand, you need to read the first two lines and only the first two lines. So I don't see any need for anything fancy.


    Sam Hobbs
    SimpleSamples.Info

    Monday, February 5, 2018 12:53 AM
  • How is "reader1" logically determined to read the second line ?

    although it works, don't mind my curiosity.

    The same way computers have worked for half a century. It reads a line and then remembers where the end of the line is. Then it reads another line starting from where it stopped. That is a simplification of course but you don't need the details. That is what a stream is; you begin at the beginning and then go only forward to the end.

    I didn't use (!result.ToLower() != "false") method,
    I used the same method I have and it worked

    So is it safe If I don't use (ToLower) ?
    or would be recommended to use it in any case?
    and what's the difference?

    thank you so much!

    There are often many ways to do something. What you have is good except you can remove:

    if (enabled == "true")

    The "else" means that the statements (technically the block) that follows is executed only if the "if" condition is false. So if enabled is not false then you know it is true.

    The ToLower will protect you if "false" is written with one or more capitals. Without the ToLower if enabled is "False" then it will either be true or at least not false. In programming it is often good to protect an application from assumptions being wrong. You are assuming that it will always be specified with lower case and that is fine if you can be sure (if a program sets it and is never changed) but if the data is coming from people then mistakes happen.



    Sam Hobbs
    SimpleSamples.Info

    Monday, February 5, 2018 1:16 AM
  • You can use Stream Reader for the above issue

    string inputString;

    string url="<your file path>"; using (StreamReader streamReader = File.OpenText(url)) { inputString = streamReader.ReadLine(); //inputString varible contains the value }


    Monday, February 5, 2018 5:40 AM
  • Simple Samples

    I really appreciate all the details you wrote, you have raised my knowledge.

    Thank you so much for explaining !

    Monday, February 5, 2018 7:47 AM
  • by the way, 
    I found that this contains the operand "!", which cannot be applied to a string.
    if (!result.ToLower() != "false")
    
    //it should be like:
    if (result.ToLower() == "false") // right?


    • Edited by Zuher Laith Monday, February 5, 2018 9:51 AM
    Monday, February 5, 2018 9:49 AM
  • I am sorry, that was a mistake. I made a quick change to the code before posting it without testing it. It should be:

    if (result.ToLower() != "false")



    Sam Hobbs
    SimpleSamples.Info

    Monday, February 5, 2018 6:22 PM
  • I am sorry, that was a mistake. I made a quick change to the code before posting it without testing it. It should be:

    if (result.ToLower() != "false")

    Correct, It did the trick, Thanks for help!
    Monday, February 5, 2018 11:04 PM