2017-01-26 32 views
0

我從鏈接下載文件,並將它們保存到目錄中。 但現在我需要將每組文件下載到特定的目錄。如何爲每個代碼所屬的名稱創建一個目錄?

這是我加入現有的項目我的項目中第一個文本文件的內容。國名。

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() 
     { 

     } 
    } 
} 
+2

太多的代碼閱讀..你可以剪出不相關的部分? – stuartd

+1

這可能應該分解成多個問題。對於第一個問題:'File.ReadLines(file1).Zip(File.ReadLines(file2),(f1,f2)=> new {DirectoryName = f1,CountryCode = f2});' – itsme86

回答

0

使用的全名。如果這兩個文件是有序的,因爲它們是在本例中,您可以創建詞典的形式代碼以完整的方式命名。

var codes = new List<string>() { ..... } 
var countries = new List<string>() { ....} 

創建字典看起來像這樣

var codeToFullNameMap = codes 
.Select((code, index) => index) 
.ToDictionary(
    keySelector: index => codes[index]. 
    elementSelector: index => cointires[index]); 

然後有創建,你可以通過它的代碼訪問完整的國名的字典。

var countryName = codeToFullNameMap["tu"]; 

關於第二個問題。如果命名爲您提供了足夠的信息,並且通過使用它認爲它的當前日期時間,則應該很好。

我的建議是爲每個下載開始時間的國家代碼和子文件夾創建目錄(包括小時,分鐘和秒鐘將足夠我猜)。

TU // (main folder) 
    -> 2017-01-26-18-50-10 // (subfolder 1) 
     -> img#1 
     -> img#2 
    -> 2017-01-26-18-50-10 // (subfolder 2) 

我不確定第三個問題。如果您的文件夾名爲TU,這足以說明圖像是針對土耳其的,或者我錯過了某些東西?每個子文件夾都包含從最後一個文件夾開始的15分鐘間隔下載的圖像。

也許如果你分享關於第三個問題的更多細節,你究竟在哪裏看到問題,例如你需要在各個國家的桌面應用程序中可視化圖像?

+0

codeToFullNameMap。這是它是如何在我的代碼:var codeToFullNameMap = codes .Select((code,index)=> index) .ToDictionary( keySelector:index => codes [index]。 valueSelector:index => countries [index ]);首先,我必須修復它沒有正確寫入的ToDictionary。其次我在valueSelector收到錯誤不存在:嚴重性\t代碼\t說明\t項目\t文件\t線\t抑制狀態 的「valueSelector的名字不在當前上下文中。 –

+0

這是工作:這是正確的方式來做到這一點? var dictionary = codes.Zip(countries,(k,v)=> new {Key = k,Value = v}) .ToDictionary(x => x.Key,x => x.Value); var countryName = dictionary [「tu」]; –

+0

是它的完美;]。關於dicionary問題,我已經更新了答案。 –

0

只需使用一個通用字典的國家代碼綁定到目錄路徑

Dictionary<string, string> countryCodeMapping = new Dictionary<string, string>() { 
    {"us", "United States"}, 
    {"is", "Isreal"} 
    ... 
}; 
相關問題