![](/img/trans.png)
[英]How can I save a list to a file then read the items back into a ListBox?
[英]How can I create List of Lists and add to it items and then read back the items?
在一流的
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 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();
}
然后在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
中将每个countryName
添加到列表中。 例如,在imagesUrls
我将具有以下格式的第一个列表:每个列表中的第一项将是国家名称,然后从每个国家名称的国家/地区代码列表构建链接。
Turkey
link1
link2
.
.
.
link9
然后转Lis
吨imagesUrls
会
Europe
link1
link2
.
.
.
link9
我要的是在创建列表imagesUrl
S和每个列表将包含在顶部首先从国名countriesnames
名单,然后使用这个国家的联系countriescodes
使用List imageUrlIrTrue
和imageUrlIrFalse
然后在Form1
我想读每个List
和每个项目List
。
这是文件countryandands.txt的一部分,以查看其格式:我通常要做的是在form1中使用此类,这样我就可以轻松获取每个国家/地区,并且所有链接都使用这两个国家/地区代码和国家/地区名称。
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("&"))
{
z2 = z2.Replace("&", " & ");
}
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;
}
}
}
}
}
我现在尝试将一个班级添加到该班级
public class Country
{
public List<Tuple<Uri, bool>> URLList { get; set; }
public string Code { get; set; }
}
不确定如何使用属性Code。 而且我在列表中添加了布尔值,因为我想在Form1中遍历列表时像在ImagesLinks方法中那样具有true和false链接:
string imageUrlIrTrue = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "true";
string imageUrlIrFalse = firstUrlPart + countryCode + secondUrlPart + DatesAndTimes[cnt] + thirdUrlPart + "false";
但是,也许应该在列表中使用bool并使其成为Tuple,我应该将其更改回List并以其他方式使用bool来建立链接。
总的来说,我希望在form1中我能够获得3个参数:
图片链接。
国家代码。
链接应为true和false,不是布尔值,而是链接的一部分,例如:
imageUrlIrTrue:
http://www.sat24.com/image2.ashx?region=is&time=201701161900&ir=true
错误:
http://www.sat24.com/image2.ashx?region=is&time=201701161900&ir=false
每个国家/地区都有9个链接,其中包含国家/地区代码以及日期和时间。 因此,在form1中,我应该具有类似列表的列表。 例如,类Country应该具有一个属性,如果我在form1中键入例如:Country.Country.Turkey
然后我应该以某种方式还拥有土耳其的9个链接和土耳其的代码。
不仅获得国家和代码,而且每个国家的每个9个链接实际上每个国家18个链接,因为每个链接都是对的true / false。
所以每个国家都有18个链接! 而且我应该能够在Form1中循环访问它们,还可以访问国家/地区代码和名称。
因此,在form1中,我应该有一个循环,可以轻松获取国家/地区名称,使每个国家/地区获得18个链接(true和false)以及每个国家/地区代码!
从技术上讲,这可能不是答案,但是更好的方法是创建一个名为“国家”的类,并为其提供url,国家名称等属性。然后,您只需要创建一个(国家)列表并填充每个列表即可。
** Daniel de junior的注意事项(如果可以向我提供countryandcodes.txt的外观,我将用代码示例进行编辑)
我想要的是在imagesUrls中创建列表,每个列表将首先包含countrynames List中的国家名称,然后使用countryscodes使用imageUrlIrTrue和imageUrlIrFalse来使用该国家/地区的链接
不确定“我要在imagesUrls中创建列表”的实际含义是什么。
我的问题是,为什么要这样做? 您真正需要做的是使用第一个编程,除了您将需要考虑所需的类之外。
这是一个示例:假设您有每个国家/地区的图片网址列表
class Country
{
List<URL> URLList;
}
国家也有国家代码
class Country
{
public List<URL> URLList{get;set;}
public string Code {get;set;}
}
现在,您可以抓住一个国家并遍历网址。 尝试使用一些类并修改您的问题,在更新时ping我,然后我将修改此答案以匹配该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.