Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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,我正在从链接下载文件,目前为止我将它们保存在一个目录中。 但是现在我需要将每组文件下载到一个特定的目录 这是我作为现有项添加到项目中的第一个文本文件的内容。国家名称 Europe Alps Benelux Germany Spain & Portugal France Greece Italy Poland Scandinavia Turkey UK & Ireland Russia Baltic Balkan Romania & Bulgaria Hungary Afr

我正在从链接下载文件,目前为止我将它们保存在一个目录中。 但是现在我需要将每组文件下载到一个特定的目录

这是我作为现有项添加到项目中的第一个文本文件的内容。国家名称

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
因此,我知道在链接中,part region=意味着本例中的国家代码是:'is'(以色列)

因此,现在我需要找到所有的链接,有代码'是'应该下载到以色列的目录

下一步,链接将与其他区域连接,例如:

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是针对土耳其的,所以现在每个带有代码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()
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统诊断;
使用System.IO;
Net系统;
使用System.Text.RegularExpressions;
命名空间下载程序
{
公共部分类Form1:Form
{
int countCompleted=0;
ExtractImages ei=新的ExtractImages();
List newList=新列表();
List countryList=新列表();
List countriesPaths=新列表();
私有队列_downloadURL=new Queue();
公共表格1()
{
初始化组件();
ManageDirectories();
lblDownloads.Text=“0”;
ei.Init();
foreach(提取图像。ei中的大陆。世界。大陆)
{
foreach(提取图像。大陆中的国家。国家)
{
如果(country.name==“以色列”)
{
foreach(国家/地区中的字符串imageUri.imageUrls)
{
countryList.Add(imageUri);
}
}
其他的
{
foreach(国家/地区中的字符串imageUri.imageUrls)
{
添加(imageUri);
}
}
}
}
}
私有void ManageDirectories()
{
字符串savedImagesPath=Path.GetDirectoryName(Application.LocalUserAppDataPath);
字符串mainPath=“国家”;
mainPath=Path.Combine(savedImagesPath,mainPath);
string[]lines=File.ReadAllLines(“CountriesNames.txt”);
foreach(行中的字符串路径)
{
字符串countryPath=Path.Combine(mainPath,Path);
如果(!Directory.Exists(countryPath))
{
CreateDirectory(countryPath);
}
countriespath.Add(countryPath);
}
字符串[]countriesCodes=File.ReadAllLines(“countriesCodes.txt”);
}
私有void下载文件(IEnumerable URL)
{
foreach(url中的变量url)
{
_下载url.Enqueue(url);
}
//开始下载
btnStart.Text=“下载…”;
btnStart.Enabled=false;
pbStatus.Visible=true;
下载文件();
}
整数计数=0;
私有void下载文件()
{
if(_downloadUrls.Any())
{
WebClient客户端=新的WebClient();
client.DownloadProgressChanged+=客户端\ u DownloadProgressChanged;
client.DownloadFileCompleted+=client_DownloadFileCompleted;
var url=_downloadUrls.Dequeue();
//字符串文件名=url.Substring(url.LastIndexOf(“/”)+1,
//(url.Length-url.LastIndexOf(“/”)-1);
client.DownloadFileAsync(新Uri(url),countriespath[count]+.gif”);
AppendText(richTextBox1,“下载:”,Color.Red);
AppendText(richTextBox1,url,Color.Green);
richTextBox1.AppendText(Environment.NewLine);
计数++;
返回;
}
//下载结束
btnStart.Text=“下载完成”;
countCompleted=newList.Count;
lblDownloads.Text=countCompleted.ToString();
定时器1.Enabl
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)