locked
Compare IP Address using File RRS feed

  • Question

  • User1109811461 posted

    Hi all,

    i using ReadLines to search IPV4 address in file, but its not work as expected. need help from expert here. Below is my code.

    Thanks

    For Each line As String In File.ReadLines("D:\IP.txt")
    If line.CompareTo(IP) Then

    Response.Write(" Detect IP")
    Else
    Response.Write(" NOT Detect IP")

    End If
    Next line

    Wednesday, January 24, 2018 12:27 AM

Answers

  • User475983607 posted

    The string.CompareTo() method returns the position of a string within another string. 

    See the docs. https://msdn.microsoft.com/en-us/library/35f0x18w(v=vs.110).aspx

    The only time string.CompareTo() returns 0 (false) is when the line exactly matches the IP variable.  Otherwise, the method always returns a non-zero value or true.

    You can figure this out on your own by learning how to use the Visual Studio debugger.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 12:44 AM
  • User1109811461 posted

    Hi  mgebhard,

    Thanks for replied, basically instead of use If line.CompareTo(IP) i'm using If line.Equals(IP) and its work. However your post gave me some idea to resolve my problem.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 2:35 AM

All replies

  • User475983607 posted

    The string.CompareTo() method returns the position of a string within another string. 

    See the docs. https://msdn.microsoft.com/en-us/library/35f0x18w(v=vs.110).aspx

    The only time string.CompareTo() returns 0 (false) is when the line exactly matches the IP variable.  Otherwise, the method always returns a non-zero value or true.

    You can figure this out on your own by learning how to use the Visual Studio debugger.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 12:44 AM
  • User1109811461 posted

    Hi  mgebhard,

    Thanks for replied, basically instead of use If line.CompareTo(IP) i'm using If line.Equals(IP) and its work. However your post gave me some idea to resolve my problem.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 24, 2018 2:35 AM