You can't get the NextIP from the NextIPAddress method.



  • class IpAddress
    {
        public readonly IPAddress FirstIp;
        public readonly IPAddress LastIp;
    
    
        public IpAddress(string firstIp,string lastIp)
        {
            FirstIp = IPAddress.Parse(firstIp);
            LastIp = IPAddress.Parse(lastIp);
        }
    
    
    
    
        public IEnumerable<IPAddress> NextIpAddress()
        {
    
            var firstIpAddressAsBytesArray = FirstIp.GetAddressBytes();
            var lastIpAddressAsBytesArray = LastIp.GetAddressBytes();
            Array.Reverse(firstIpAddressAsBytesArray);
            Array.Reverse(lastIpAddressAsBytesArray);
            var firstIpAddressAsInt = BitConverter.ToInt32(firstIpAddressAsBytesArray, 0);
            var lastIpAddressAsInt = BitConverter.ToInt32(lastIpAddressAsBytesArray, 0);
            for (var i = firstIpAddressAsInt; i <= lastIpAddressAsInt; i++)
            {
                var bytes = BitConverter.GetBytes(i);
                yield return  new IPAddress(new[] { bytes[3], bytes[2], bytes[1], bytes[0] });
    
            }
    
        }
    
    }
    

    (foreach not to propose) in this case, such a method works

    I want the method to give the next IP address and record it in the file.

    private void button1_Click(object sender, EventArgs e)
    

    {
    var b = a.NextIpAddress().GetEnumerator();
    b.MoveNext();
    Log.WriteToFile("LogFile.txt",b.Current.ToString());
    }

    only the first IP address is recorded in this code.
    The first IP Address (although MoveNext is available) was also recorded when repressed and wanted to be the next from the range.
    I've tried everything, please.



  • You create every time b - that's how it starts counting. Take away the creature. b From the method (take a variable field, and fill it out somewhere else) b.MoveNext() and work as you wish.

    By the way, don't forget to check the return value so you don't miss the end of the sequence.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2