/// <summary> /// New DownloadToDir overload that takes a new ImageLink class, this is used to retain the image's uploaded filename. /// </summary> public static bool DownloadToDir(ImageLink link, string dir) { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string destFilepath = CombinePathAndFilename(dir, link.GenerateNewFilename((ImageFileNameFormat)Properties.Settings.Default.ImageFilenameFormat)); try { WebClient webClient = new WebClient(); webClient.DownloadFile(link.URL, destFilepath); // TODO: Figure out how to make the async downloading work properly. // This line is currently not working and downloads 0 byte files 100% of the time. //webClient.DownloadFileAsync(new Uri(link.URL), destFilepath); /*Task.Run(() => { * webClient.DownloadFileAsync(new Uri(link.URL), destFilepath); * while (webClient.IsBusy) * { * //Do nothing? * } * });*/ //new Thread(() => { webClient.DownloadFile(link.URL, destFilepath); }).Start(); return(true); } catch (WebException WebE) { return(false); throw WebE; } }
/// <summary> /// New DownloadToDir overload that takes a new ImageLink class, this is used to retain the image's uploaded filename. /// </summary> public static bool DownloadToDir(ImageLink link, string dir) { //Program.Log(true, $"DownloadToDir called for link {link.URL} downloading to {dir}"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string destFilepath = CombinePathAndFilename(dir, link.GenerateNewFilename((ImageFileNameFormat)Properties.Settings.Default.imageFilenameFormat)); int formatCount = Enum.GetValues(typeof(ImageFileNameFormat)).Length; List <ImageFileNameFormat> formatsNotInUse = new List <ImageFileNameFormat>(); for (int i = 0; i < formatCount; i++) { if (i != Properties.Settings.Default.imageFilenameFormat) { formatsNotInUse.Add((ImageFileNameFormat)i); } } try { // Incase imageFileNameFormat has changed and the file is already saved, instead of downloading another copy with a // new name, check if it already exists and if it does then rename it to the new imageFileNameFormat. for (int i = 0; i < formatsNotInUse.Count; i++) { string potentialFilepath = CombinePathAndFilename(dir, link.GenerateNewFilename(formatsNotInUse[i])); if (File.Exists(potentialFilepath)) { File.Move(potentialFilepath, destFilepath); return(true); } } //if (!File.Exists(destFilepath) || new System.IO.FileInfo(destFilepath).Length < 1) if ((File.Exists(destFilepath) && new System.IO.FileInfo(destFilepath).Length < 1) || !File.Exists(destFilepath)) { WebClient webClient = new WebClient(); webClient.DownloadFile(link.URL, destFilepath); //TODO: Figure out how to make the async downloading work properly. // This line is currently not working and downloads 0 byte files 100% of the time. //webClient.DownloadFileAsync(new Uri(link.URL), destFilepath); /*Task.Run(() => { * webClient.DownloadFileAsync(new Uri(link.URL), destFilepath); * while (webClient.IsBusy) * { * //Do nothing? * } * });*/ //new Thread(() => { webClient.DownloadFile(link.URL, destFilepath); }).Start(); return(true); } return(false); } catch (WebException WebE) { return(false); throw WebE; } }