Exemplo n.º 1
0
        /// <summary>
        /// 渡されたアドレスリストを元に画像のダウンロードを開始する
        /// </summary>
        /// <param name="imageAdresses">ダウンロード対象画像アドレス</param>
        /// <param name="folder">ダウンロード先ディレクトリ</param>
        public void StartDownload(string imageAdresse, string folder)
        {
            if (folder.EndsWith(@"\") == false)
            {
                folder = folder + @"\";
            }

            WebClient wc = new WebClient();

            string downloadingfileName = imageAdresse;

            //禁則文字の置換
            foreach (var InvalidPathChar in Path.GetInvalidPathChars())
            {
                if (imageAdresse.Contains(InvalidPathChar.ToString()))
                {
                    downloadingfileName = downloadingfileName.Replace(InvalidPathChar, '_');
                }
            }

            downloadingfileName = Path.GetFileName(downloadingfileName);

            //禁則文字の置換
            foreach (var InvalidPathString in invalidPathStrings)
            {
                if (imageAdresse.Contains(InvalidPathString))
                {
                    downloadingfileName = downloadingfileName.Replace(InvalidPathString, "_");
                }
            }

            string fileName = folder + downloadingfileName;
            int    count    = 1;

            //保存名が既に存在する場合、末尾にナンバリングを行い重複しないようにする
            while (System.IO.File.Exists(fileName))
            {
                string extension = Path.GetExtension(fileName);

                fileName = fileName.Remove(fileName.Length - extension.Length, extension.Length);

                if (count != 1)
                {
                    fileName = fileName.Remove(fileName.Length - count.ToString().Length, count.ToString().Length);
                }

                fileName = fileName + count.ToString() + extension;

                count++;
            }

            wc.DownloadFile(new Uri(imageAdresse), fileName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces invalid Characters (Not acceptable by Windows while renaming a file or Path) with '_' underscore.
        /// </summary>
        /// <param name="fileName">File Name</param>
        /// <returns>Returns Valid Windows File Name.</returns>
        public static string GetValidFileName(string fileName)
        {
            string ReturnValue = fileName;

            foreach (char InvalidFileNameChar in Path.GetInvalidFileNameChars())
            {
                ReturnValue = ReturnValue.Replace(InvalidFileNameChar, '_');
            }

            foreach (char InvalidPathChar in Path.GetInvalidPathChars())
            {
                ReturnValue = ReturnValue.Replace(InvalidPathChar.ToString(), string.Empty);
            }

            return(ReturnValue);
        }