从文本文件c#中获取值

本文关键字:获取 文本 文件 | 更新日期: 2023-09-27 18:09:05

我目前正在开发一个c#应用程序,我需要从txt文件中恢复socks的值,这里是txt文件中的信息

104.131.163.123:2541
104.131.178.167:2541

我需要逐行读取文件,并检索IP和端口值的值,并将它们放在一个列表

this my code:

我需要逐行读取文件,并检索IP和端口值的值,并将它们放在一个列表中这是我的代码

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);
            }

谢谢

从文本文件c#中获取值

下面的代码读取文件的所有行,并将每个ip地址添加到一个列表

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;
}