-3

I am currently developing an application in C #, I need to recover the values of socks from a txt file here are the information in the txt file

104.131.163.123:2541
104.131.178.167:2541

I need to read the file line by line for each row and retrieve the value of the IP and the port value and put them in a list

this my code :

I need to read the file line by line for each row and retrieve the value of the IP and the port value and put them in a list this is my code

List<string[]> list  = new List<string[]>();
            StreamReader sr = new StreamReader (@"C:\");
            string line;
            while ((line = sr.ReadLine()) != null)`enter code here`
            {
                string[] array = line.Spit(":");
                list.Add(array);
            }

Thank you

  • 1
    Hwat have you tried so far? Where are you stuck? There are plenty of tutorials on reading text files (including the [MSDN documentation](https://msdn.microsoft.com/en-us/library/db5x7c0d.aspx)) – D Stanley Aug 20 '15 at 22:17
  • i have search in msdn in multiple book but no result – user3537566 Aug 20 '15 at 22:18
  • 1
    did you first search [how to read from txt file](https://www.google.com/search?q=+how+to+read+from+txt+file+c%23&ie=utf-8&oe=utf-8)? – M.kazem Akhgary Aug 20 '15 at 22:18
  • I think you're confusing Stack Overflow with a write-my-code-for-me site. We're here to assis when you have a problem, not write code based on a spec you provide. – Craig W. Aug 20 '15 at 22:18
  • [I think this is what you need](http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) – M.kazem Akhgary Aug 20 '15 at 22:19
  • i have modified , i not want to write code for me , i need a help Craig W. – user3537566 Aug 20 '15 at 22:28

1 Answers1

1

Following code reads all lines of a file and add each ip address into a list

private List<string> GetIPAddress()
{
    var list = new List<string>();
    var input = File.ReadAllText("file.txt");
    var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
    foreach (Match match in r.Matches(input))
    {
         string ip = match.Groups[1].Value;
         string port = match.Groups[2].Value;
         list.Add(ip);
         // you can also add port in the list
     }
     return list;
}
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110