Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建列表并向其中添加项目,然后重新读取项目?_C#_.net_Winforms - Fatal编程技术网

C# 如何创建列表并向其中添加项目,然后重新读取项目?

C# 如何创建列表并向其中添加项目,然后重新读取项目?,c#,.net,winforms,C#,.net,Winforms,名列前茅 public static List<string> countriescodes = new List<string>(); public static List<string> countriesnames = new List<string>(); public static List<List<string>> imagesUrls = new List<List<string>>(

名列前茅

public static List<string> countriescodes = new List<string>();
public static List<string> countriesnames = new List<string>();
public static List<List<string>> imagesUrls = new List<List<string>>();
然后在ImagesLinks中

public void ImagesLinks()
{
     int cnt = 0;
     foreach (string countryCode in countriescodes)
      {
          cnt++;
          for (; cnt < DatesAndTimes.Count(); cnt++)
          {
              string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
              string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
              imagesUrls.Add(imageUrlIrTrue);
              imagesUrls.Add(imageUrlIrFalse);
              if (cnt % 10 == 0) break;
           }
      }
 }
然后将显示
imagesUrls
中的ext
Lis
t

Europe

link1
link2
.
.
.
link9
我想要的是在
imagesUrl
s中创建列表,每个列表的顶部首先包含
countriesnames
列表中的国家名称,然后使用
countriescodes
列表中的
imageUrlIrTrue
imageUrlIrFalse

然后在
Form1
中,我想阅读每个
列表以及每个
列表的项目

这是countriesandcodes.txt文件的一部分,用于查看其格式: 一般来说,我想做的是在form1中使用这个类,这样我就可以很容易地得到每个国家及其所有链接,使用列表countriescodes和countriesnames

Country Code = eu
Country Name = Europe
Country Code = alps
Country Name = Alps
Country Code = nl
Country Name = Benelux
Country Code = de
Country Name = Germany
Country Code = sp
Country Name = Spain & Portugal
Country Code = fr
Country Name = France
这是该类的完整代码。也许这样看比较容易

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Xml;
using HtmlAgilityPack;
using System.ComponentModel;

namespace SatelliteImages
{
    class ExtractImages
    {
        static WebClient client;
        static string htmltoextract;
        public static List<string> countriescodes = new List<string>();
        public static List<string> countriesnames = new List<string>();
        public static List<string> DatesAndTimes = new List<string>();
        public static List<List<string>> imagesUrls = new List<List<string>>();

        static string firstUrlPart = "http://www.sat24.com/image2.ashx?region=";
        static string secondUrlPart = "&time=";
        static string thirdUrlPart = "&ir=";
        StreamWriter w;
        string[] lines;

        public bool WebProblem = false;

        public void Init()
        {
            if (!File.Exists(@"c:\temp\countriesandcodes.txt"))
            {
                w = new StreamWriter(@"c:\temp\countriesandcodes.txt");
            }
            else
            {
                lines = File.ReadAllLines(@"c:\temp\countriesandcodes.txt");
            }

            foreach (string countrycode in lines)
            {
                if (countrycode.Contains("Code"))
                {
                    string code = countrycode.Substring(15);
                    countriescodes.Add(code);
                }
                else
                {
                    string code = countrycode.Substring(15);
                    countriesnames.Add(code);
                }
            }

            foreach (string cc in countriescodes)
            {
                ExtractDateAndTime("http://www.sat24.com/image2.ashx?region=" + cc);
            }
            ImagesLinks();
        }     

        public void ExtractCountires()
        {
            try
            {
                htmltoextract = "http://sat24.com/en/?ir=true";//"http://sat24.com/en/";// + regions;
                client = new WebClient();
                client.DownloadFile(htmltoextract, @"c:\temp\sat24.html");
                client.Dispose();

                string tag1 = "<li><a href=\"/en/";
                string tag2 = "</a></li>";

                string s = System.IO.File.ReadAllText(@"c:\temp\sat24.html");
                s = s.Substring(s.IndexOf(tag1));
                s = s.Substring(0, s.LastIndexOf(tag2) + tag2.ToCharArray().Length);
                s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");

                string[] parts = s.Split(new string[] { tag1, tag2 }, StringSplitOptions.RemoveEmptyEntries);


                string tag3 = "<li><ahref=\"/en/";

                for (int i = 0; i < parts.Length; i++)
                {
                    if (i == 40)
                    {
                        break;
                    }
                    string l = "";
                    if (parts[i].Contains(tag3))
                        l = parts[i].Replace(tag3, "");

                    if (i == 39)
                    {
                        string fff = "";
                    }
                    string z1 = l.Substring(0, l.IndexOf('"'));
                    if (z1.Contains("</ul></li><liclass="))
                    {
                        z1 = z1.Replace("</ul></li><liclass=", "af");
                    }
                    countriescodes.Add(z1);
                    countriescodes.GroupBy(n => n).Any(c => c.Count() > 1);

                    string z2 = parts[i].Substring(parts[i].LastIndexOf('>') + 1);
                    if (z2.Contains("&amp;"))
                    {
                        z2 = z2.Replace("&amp;", " & ");
                    }
                    countriesnames.Add(z2);
                    countriesnames.GroupBy(n => n).Any(c => c.Count() > 1);
                }

                for (int i = 0; i < countriescodes.Count; i++)
                {
                    w.WriteLine("Country Code = " + countriescodes[i]);
                    w.WriteLine("Country Name = " + countriesnames[i]);
                }
                w.Close();
            }
            catch (Exception e)
            {
                if (countriescodes.Count == 0)
                {
                    /*countriescodes = new List<string>();
                    countriesnames = new List<string>();
                    DatesAndTimes = new List<string>();
                    imagesUrls = new List<string>();
                    Init();*/
                }
            }
        }

        public void ExtractDateAndTime(string baseAddress)
        {
            try
            {
                var wc = new WebClient();
                wc.BaseAddress = baseAddress;
                HtmlDocument doc = new HtmlDocument();

                var temp = wc.DownloadData("/en");
                doc.Load(new MemoryStream(temp));

                var secTokenScript = doc.DocumentNode.Descendants()
                    .Where(e =>
                           String.Compare(e.Name, "script", true) == 0 &&
                           String.Compare(e.ParentNode.Name, "div", true) == 0 &&
                           e.InnerText.Length > 0 &&
                           e.InnerText.Trim().StartsWith("var region")
                          ).FirstOrDefault().InnerText;
                var securityToken = secTokenScript;
                securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));
                securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", "");
                var dates = securityToken.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                var scriptDates = dates.Select(x => new ScriptDate { DateString = x });
                foreach (var date in scriptDates)
                {
                    DatesAndTimes.Add(date.DateString);
                }
            }
            catch(WebException wex)
            {
                WebProblem = true;
            }
        }

        public class ScriptDate
        {
            public string DateString { get; set; }
            public int Year
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(0, 4));
                }
            }
            public int Month
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(4, 2));
                }
            }
            public int Day
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(6, 2));
                }
            }
            public int Hours
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(8, 2));
                }
            }
            public int Minutes
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(10, 2));
                }
            }
        }

        public void ImagesLinks()
        {
            int cnt = 0;
            foreach (string countryCode in countriescodes)
            {
                cnt++;
                for (; cnt < DatesAndTimes.Count(); cnt++)
                {
                    string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
                    string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
                    imagesUrls.Add(imageUrlIrTrue);
                    imagesUrls.Add(imageUrlIrFalse);
                    if (cnt % 10 == 0) break;
                }
            }
        }
    }
}
但也许我应该改为在列表中使用bool并使其成为Tuple,我应该将其改回List,并以其他方式使用bool来构建链接

一般来说,我希望在表格1中,我能够得到3个参数:

  • 图片链接

  • 国家代码

  • 链接应为true和false,而不是bool,而是作为链接的一部分,例如:

  • imageUrlIrTrue:

    和错误:

    每个国家都有9个由国家代码、日期和时间组成的链接。 所以在表格1中,我应该有一个类似于列表的列表。 例如,如果我在form1:Country.Country.Turkey中键入,则类Country应该有一个属性

    然后我应该也有土耳其的9个链接和土耳其的代码

    这不仅是为了获得国家和代码,而且每个国家的9个链接实际上是每个国家的18个链接,因为每个链接都是双正确/错误

    所以每个国家都有18个链接!!!我应该可以在form1中循环访问它们,也可以访问国家代码和名称


    所以在表格1中,我应该有一个循环,我可以很容易地得到国家名称,每个国家有18个链接(对和错)和每个国家代码

    从技术上讲,这可能不是答案,但更好的方法是创建一个名为“Country”的类,并为其提供url、Country name等属性。然后您只需要创建一个(国家)列表并填充每个国家

    **给Daniel d e junior的说明
    (如果你能提供countriesandcodes.txt的样子,我会用一个代码示例对其进行编辑)

    从技术上讲,这可能不是答案,但更好的方法是创建一个名为“Country”的类,并为其提供url、Country name等属性。然后,你只需要创建一个(国家)列表并填充每个国家

    **给Daniel d e junior的说明 (如果您能提供countriesandcodes.txt的外观,我将用代码示例编辑此内容)

    我想要的是在imagesUrls中创建列表,每个列表的顶部首先包含countriesnames列表中的国家名称,然后是使用imageUrlIrTrue和imageUrlIrFalse的countriescodes列表中该国家的链接

    我不知道“我想要的是在imagesUrls中创建列表”实际上意味着什么

    我的问题是你为什么要这样做?您真正需要做的是首先使用编程,除非您需要考虑您需要什么类

    以下是一个例子: 假设您有每个国家的图像url列表

    class Country
    {
      List<URL> URLList;
    }
    
    国家级
    {
    列表URL列表;
    }
    
    国家也有国家代码

    class Country
    {
      public List<URL> URLList{get;set;}
      public string Code {get;set;}
    }
    
    国家级
    {
    公共列表URLList{get;set;}
    公共字符串代码{get;set;}
    }
    
    现在你可以抓取一个国家并通过它的url循环。 尝试使用一些类并修改您的问题,在更新时ping我,我将修改此答案以匹配问题

    我想要的是在imagesUrls中创建列表,每个列表的顶部首先包含countriesnames列表中的国家名称,然后是使用imageUrlIrTrue和imageUrlIrFalse的countriescodes列表中该国家的链接

    我不知道“我想要的是在imagesUrls中创建列表”实际上意味着什么

    我的问题是你为什么要这样做?您真正需要做的是首先使用编程,除非您需要考虑您需要什么类

    以下是一个例子: 假设您有每个国家的图像url列表

    class Country
    {
      List<URL> URLList;
    }
    
    国家级
    {
    列表URL列表;
    }
    
    国家也有国家代码

    class Country
    {
      public List<URL> URLList{get;set;}
      public string Code {get;set;}
    }
    
    国家级
    {
    公共列表URLList{get;set;}
    公共字符串代码{get;set;}
    }
    
    现在你可以抓取一个国家并通过它的url循环。
    尝试使用一些类并修改您的问题,在更新时给我打电话,我将修改此答案以匹配问题。

    因为我们不知道
    countries和codes.txt
    的样子,所以很难给出好的答案。从您发布的关于您的问题的内容。。。我同意@Gavin Perkins的观点,它看起来很方便。如果有任何原因,你的
    countriesandcodes.txt
    文件不是标准的、众所周知的格式(如XML或JSON)?@DanielMann不是真正的原因。我刚创建文件时添加了国家代码。我可以将其更改为将创建文件的部分添加到我的问题中。我不再创建该文件了,它只创建了一次。@您能将文本文件用管道或逗号分隔吗?(示例:fr | france us | United States)我更改了文本文件,现在它是csv类型:欧洲、欧盟阿尔卑斯山、阿尔卑斯比荷卢、nl德国、西班牙和葡萄牙、sp法国、fr希腊、gr意大利、it波兰、pl斯堪的纳维亚、SCAN因为我们不知道
    国家和Codes.txt
    看起来像什么,所以很难给出一个好的答案。从您发布的关于您的问题的内容。。。我同意@Gavin Perkins的观点,看起来一个类会派上用场。如果有任何原因,你的
    countriesandcodes.txt
    文件不是标准的、众所周知的文件
    class Country
    {
      List<URL> URLList;
    }
    
    class Country
    {
      public List<URL> URLList{get;set;}
      public string Code {get;set;}
    }