0

string ip; int port; here is my code that reads into the text file IEnumerable<String> lines = File.ReadLines("proxies.txt"); I want to sort IP and PORT in the code(above) by reading them from the text file as IP:PORT and use them for my string/int. I want to access them instantaneously.

Only one IP:PORT can be used at one time.(I guess they can be some of of an array as [0] and adds 1 for each line each time its cycled) The list file is very simple heres a visual

ip1:port1
ip2:port2
ip3:port3
ip4:port4
  • Sort them how?? You've only declared simple variables for host and port rather than collections; assuming you've read multiple lines, how do you wish to store them in single variables? – Caius Jard May 07 '19 at 07:35
  • @Caius Jard I need them to be changed upon a certain condition. – JordanPetersonFan May 07 '19 at 07:37
  • Youre not really giving enough information to effectively answer your question – Caius Jard May 07 '19 at 07:37
  • Sorry for the confusion! I need to take one from the list. and the rest will be cycled though once a condition is met. – JordanPetersonFan May 07 '19 at 07:38
  • Which one do you want to take? – Caius Jard May 07 '19 at 07:38
  • probably the first one – JordanPetersonFan May 07 '19 at 07:39
  • What does "each time it's cycled" mean? You seem to keep changing your mind about what you want to do, and add a bit more info each time - remember that you know your end goal, we only have what you tell us, so what youre trying to achieve is a relative mystery to us – Caius Jard May 07 '19 at 07:44
  • Have a look at: https://stackoverflow.com/questions/4968795/ipaddress-parse-using-port-on-ipv4 or https://stackoverflow.com/questions/2727609/best-way-to-create-ipendpoint-from-string/35357209 – Rand Random May 07 '19 at 07:57

2 Answers2

0

To read the first line (for now; i'll leave the read-all-lines code in so you can choose different lines in future, but see below for an addition if your file is large) and split it on colon, taking the first bit as the ip and the second bit as the port:

string[] lines = File.ReadAllLines("path");
string[] bits = lines[0].Split(':');
string host = bits[0];
int port = int.Parse(bits[1]);

There is no error handling here - if the file has zero lines, or if the first line has no colon on it, or if the data after the colon is not numeric, you'll get an exception..

If your file is huge, using ReadLines() might be better, as it doesn't read the entire file in one go; it reads gradually as you pull from it. Something like this:

IEnumerable<string> lines = File.ReadLines("path");
string[] bits = lines.First().Split(':');
string host = bits[0];
int port = int.Parse(bits[1]);
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

Here you can cycle all the lines in the text.

        string line;
        System.IO.StreamReader file = new System.IO.StreamReader(@"C:\test.txt");

        while ((line = file.ReadLine()) != null)
        {
            var data = line.Split(':');
            string ipAdd = data[0];
            int port = int.Parse(data[1]);

            //Your conditions here...
            Console.WriteLine(ipAdd);
            Console.WriteLine(port);
        }
SerMarvz
  • 153
  • 2
  • 14