Beispiel #1
0
        public void TestChangeExtension()
        {
            var filename = Path.Combine(longPathDirectory, "filename.ext");
            var expectedFilenameWithNewExtension = Path.Combine(longPathDirectory, "filename.txt");

            Assert.AreEqual(expectedFilenameWithNewExtension, Path.ChangeExtension(filename, ".txt"));
        }
Beispiel #2
0
        private void RecursivelyRetryDownload(string downloadURL, ref string tempFilePath, int count, int maxretry)
        {
            try
            {
                // download image
                if (downloadURL.Length <= 0)
                {
                    return;
                }
                using (WebClient client = new WebClient())
                {
                    client.Headers.Add("user-agent", "JMM");
                    //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));
                    //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0}  *** to ***  {1}", req.URL, fullName);

                    byte[] bytes = client.DownloadData(downloadURL);
                    if (bytes.Length < 4)
                    {
                        throw new WebException(
                                  "The image download stream returned less than 4 bytes (a valid image has 2-4 bytes in the header)");
                    }

                    ImageFormatEnum imageFormat = Misc.GetImageFormat(bytes);
                    string          extension;
                    switch (imageFormat)
                    {
                    case ImageFormatEnum.bmp:
                        extension = ".bmp";
                        break;

                    case ImageFormatEnum.gif:
                        extension = ".gif";
                        break;

                    case ImageFormatEnum.jpeg:
                        extension = ".jpeg";
                        break;

                    case ImageFormatEnum.png:
                        extension = ".png";
                        break;

                    case ImageFormatEnum.tiff:
                        extension = ".tiff";
                        break;

                    default: throw new WebException("The image download stream returned an invalid image");
                    }

                    if (extension.Length <= 0)
                    {
                        return;
                    }
                    string newFile = Path.ChangeExtension(tempFilePath, extension);
                    if (newFile == null)
                    {
                        return;
                    }

                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    tempFilePath = newFile;
                }
            }
            catch (WebException)
            {
                if (count + 1 >= maxretry)
                {
                    throw;
                }
                Thread.Sleep(500);
                RecursivelyRetryDownload(downloadURL, ref tempFilePath, count + 1, maxretry);
            }
        }
Beispiel #3
0
 public static string ChangeExtension(string filename, string extension)
 {
     return(Path.ChangeExtension(filename, extension));
 }