我從鏈接下載文件,並將它們保存到目錄中。 但現在我需要將每組文件下載到特定的目錄。如何爲每個代碼所屬的名稱創建一個目錄?
這是我加入現有的項目我的項目中第一個文本文件的內容。國名。
Europe
Alps
Benelux
Germany
Spain & Portugal
France
Greece
Italy
Poland
Scandinavia
Turkey
UK & Ireland
Russia
Baltic
Balkan
Romania & Bulgaria
Hungary
Africa
Algeria
Cameroon
CanaryIslands
Congo
CentralAfrica
Nigeria
Chad
Egypt
Ethiopia
Israel
Libya
Madagascar
Morocco
Namibia
SaudiArabia
Somalia
SouthAfrica
Sudan
Tanzania
Tunesia
WestAfrica
Zambia
,我已經是目前每個國家代碼的另一個文本文件:
eu
alps
nl
de
sp
fr
gr
it
pl
scan
tu
gb
ru
bc
ba
se
hu
af
dz
cm
ce
cg
caf
ng
td
eg
et
is
ly
mg
mo
bw
sa
so
za
sd
tz
tn
wa
zm
爲什麼代碼很重要?因爲每個鏈接都是用國家代碼而不是名稱構建的。例如:
http://www.sat24.com/image2.ashx?region=is&time=201612271600&ir=true
,所以我知道,在鏈接部分區域=的意思是,在這種情況下,國家代碼是:「是」(以色列)。
所以現在我需要找到所有具有代碼的鏈接「是」應該被下載到以色列的目錄。
下一頁時,鏈接將與其他地區例如:
http://www.sat24.com/image2.ashx?region=tu&time=201612271600&ir=true
所以,現在爲國家土耳其的代碼恩是這樣,現在的代碼TU應下載到土耳其目錄中的每一個環節。
所以,第一個問題是如何將國家的代碼之間的聯繫,以國名和連接,然後將其下載到我媒體鏈接在構造函數中創建了正確的國家名字的目錄?我有所有的國家名稱目錄allready,但我需要以某種方式連接到國家名稱目錄的鏈接中的代碼(地區)。
的第二個問題是,我使這個下載每隔15分鐘。我會稍後用計時器來做。每15分鐘再次下載圖像。但我不想刪除或覆蓋舊圖像,主要想法是保存和保存圖像。問題出現在每個國家每15分鐘我應該創建哪些子路徑?我的意思是每15分鐘給每個國家分路的名字是什麼?
我想創造每個國家與下載的圖像的日期和時間範圍的目錄,但我不知道這是否是一個好主意。
以後我會希望能夠給每一個國家的這個目錄之間,並且將圖像加載到一個PictureBox移動。問題是我將如何識別每15分鐘下載的每個目錄?
問題不在下載程序工作。 這兩個問題與目錄有關。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace Downloader
{
public partial class Form1 : Form
{
int countCompleted = 0;
ExtractImages ei = new ExtractImages();
List<string> newList = new List<string>();
List<string> countryList = new List<string>();
List<string> countriesPaths = new List<string>();
private Queue<string> _downloadUrls = new Queue<string>();
public Form1()
{
InitializeComponent();
ManageDirectories();
lblDownloads.Text = "0";
ei.Init();
foreach (ExtractImages.Continent continent in ei.world.continents)
{
foreach (ExtractImages.Country country in continent.countries)
{
if (country.name == "Israel")
{
foreach (string imageUri in country.imageUrls)
{
countryList.Add(imageUri);
}
}
else
{
foreach (string imageUri in country.imageUrls)
{
newList.Add(imageUri);
}
}
}
}
}
private void ManageDirectories()
{
string savedImagesPath = Path.GetDirectoryName(Application.LocalUserAppDataPath);
string mainPath = "Countries";
mainPath = Path.Combine(savedImagesPath, mainPath);
string[] lines = File.ReadAllLines("CountriesNames.txt");
foreach(string path in lines)
{
string countryPath = Path.Combine(mainPath, path);
if (!Directory.Exists(countryPath))
{
Directory.CreateDirectory(countryPath);
}
countriesPaths.Add(countryPath);
}
string[] countriesCodes = File.ReadAllLines("CountriesCodes.txt");
}
private void downloadFile(IEnumerable<string> urls)
{
foreach (var url in urls)
{
_downloadUrls.Enqueue(url);
}
// Starts the download
btnStart.Text = "Downloading...";
btnStart.Enabled = false;
pbStatus.Visible = true;
DownloadFile();
}
int count = 0;
private void DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;
var url = _downloadUrls.Dequeue();
//string FileName = url.Substring(url.LastIndexOf("/") + 1,
// (url.Length - url.LastIndexOf("/") - 1));
client.DownloadFileAsync(new Uri(url), countriesPaths[count] + ".gif");
RichTextBoxExtensions.AppendText(richTextBox1, "Downloading: ", Color.Red);
RichTextBoxExtensions.AppendText(richTextBox1, url, Color.Green);
richTextBox1.AppendText(Environment.NewLine);
count++;
return;
}
// End of the download
btnStart.Text = "Download Complete";
countCompleted = newList.Count;
lblDownloads.Text = countCompleted.ToString();
timer1.Enabled = true;
downloadFile(newList);
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
RichTextBoxExtensions.UpdateText(richTextBox1, "Downloading: ", "Downloaded: ", Color.Red);
// handle error scenario
throw e.Error;
}
else
{
countCompleted--;
lblDownloads.Text = countCompleted.ToString();
RichTextBoxExtensions.UpdateText(richTextBox1, "Downloading: ", "Downloaded: ", Color.Green);
}
if (e.Cancelled)
{
// handle cancelled scenario
}
DownloadFile();
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn/totalBytes * 100;
pbStatus.Value = int.Parse(Math.Truncate(percentage).ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
countCompleted = countryList.Count;
lblDownloads.Text = countCompleted.ToString();
downloadFile(countryList);
}
public class RichTextBoxExtensions
{
public static void AppendText(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static void UpdateText(RichTextBox box, string find, string replace, Color? color)
{
box.SelectionStart = box.Find(find, RichTextBoxFinds.Reverse);
box.SelectionLength = find.Length;
box.SelectionColor = color ?? box.SelectionColor;
box.SelectedText = replace;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void SortList()
{
}
}
}
太多的代碼閱讀..你可以剪出不相關的部分? – stuartd
這可能應該分解成多個問題。對於第一個問題:'File.ReadLines(file1).Zip(File.ReadLines(file2),(f1,f2)=> new {DirectoryName = f1,CountryCode = f2});' – itsme86