Example #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var setting = new DownloadSetting();
                foreach (ListViewItem li in listView1.Items)
                {
                    var web = new Website();
                    web.Url = li.SubItems[0].Text;
                    setting.Websites.Add(web);
                }

                if (setting.Websites.Count > 0)
                {
                    XmlHelper.XmlSerialize(Download.DownloadSettingFileName, setting, typeof(DownloadSetting));
                }
                else if (File.Exists(Download.DownloadSettingFileName))
                {
                    File.Delete(Download.DownloadSettingFileName);
                }

                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #2
0
        public async static Task Download(DownloadSetting downloadSetting)
        {
            WebClient _Client = new WebClient();

            _Client.DownloadProgressChanged += downloadSetting.DownloadProgressChanged;
            _Client.DownloadFileCompleted   += downloadSetting.DownloadFileCompleted;
            await _Client.DownloadFileTaskAsync(downloadSetting.DownloadUri, downloadSetting.DownloadPath);
        }
Example #3
0
    private IEnumerator DownloadProcess(DownloadSetting setting, bool useCache)
    {
        string localFilePath = localCachePath + "/" + setting.CacheRelativePath;

        if (useCache)
        {
            //ローカルにファイルがあるか?
            if (FileUtility.Exists(localFilePath))
            {
                // キャッシュファイルと時刻比較して同じならそのファイルを読み込む
                if (FileUtility.IsSameLastUpdateTime(localFilePath, setting.LastUpdateTime))
                {
                    var assets = FileUtility.ReadFromFileWith3DES(localFilePath);
                    if (assets != null)
                    {
                        if (setting.OnProgress != null)
                        {
                            setting.OnProgress(1.0f);
                        }
                        if (setting.OnLoad != null)
                        {
                            setting.OnLoad(true, assets);
                        }
                        yield break;
                    }
                }
                else
                {
                    FileUtility.Delete(localFilePath);
                }
            }
        }

        WWW www = new WWW(urlBuilder(setting.RelativeURL));

        while (!www.isDone)
        {
            if (setting.OnProgress != null)
            {
                setting.OnProgress(www.progress);
            }
            yield return(null);
        }

        //失敗
        if ((www == null) || (www.error != null))
        {
            if (setting.OnError != null)
            {
                setting.OnError((www == null) ? "" : www.error);
            }

            //成功
        }
        else
        {
            if (setting.OnProgress != null)
            {
                setting.OnProgress(1.0f);
            }
            if (setting.OnLoad != null)
            {
                setting.OnLoad(false, www.bytes);
            }

            if (useCache)
            {
                //キャッシュ保存
                FileUtility.WriteToFileWith3DES(www.bytes, localFilePath);
                FileUtility.Touch(localFilePath, setting.LastUpdateTime);
            }
        }

        if (www != null)
        {
            www.Dispose();
        }
    }
Example #4
0
 private IEnumerator DownloadAndCache(DownloadSetting setting)
 {
     yield return(DownloadProcess(setting, true));
 }
Example #5
0
 private IEnumerator DownloadOnly(DownloadSetting setting)
 {
     yield return(DownloadProcess(setting, false));
 }