Skip to content

Instantly share code, notes, and snippets.

@Taiizor
Last active September 1, 2022 16:38
Show Gist options
  • Save Taiizor/3d7de6e836a9384113e974c32c3e76b3 to your computer and use it in GitHub Desktop.
Save Taiizor/3d7de6e836a9384113e974c32c3e76b3 to your computer and use it in GitHub Desktop.
Download file
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Downloader
{
internal class Program
{
private const string Url = "https://raw.githubusercontent.com/Taiizor/ReaLTaiizor/develop/.screenshots";
private static readonly string Move = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
private static void Main()
{
DownloadFile("Splash.gif");
Console.WriteLine("Pencereyi kapatmak için bir tuşa basın..")
Console.ReadKey();
}
private static Task DownloadFile(string FileName)
{
string DownloadUrl = CombineUrl(Url, FileName);
string TempFile = Path.Combine(Path.GetTempPath(), FileName);
Console.WriteLine($"{DownloadUrl} adresinden indirme işlemi başlatılıyor.");
try
{
using WebClient WebClient = new();
WebClient.DownloadFile(DownloadUrl, TempFile);
if (File.Exists(TempFile))
{
if (File.Exists(Path.Combine(Move, FileName)))
{
Random Random = new();
File.Move(TempFile, Path.Combine(Move, $"{Random.Next(99)}-{FileName}"));
}
else
{
File.Move(TempFile, Path.Combine(Move, FileName));
}
Console.WriteLine($"Dosya başarıyla indirildi.");
}
}
catch (Exception Ex)
{
if (File.Exists(TempFile))
{
File.Delete(TempFile);
}
Console.Write($"Hata Oluştu: {Ex.Message}");
}
return Task.CompletedTask;
}
private static string CombineUrl(params string[] args)
{
IEnumerable<string> prefixAdjusted = args.Select(x => x.StartsWith("/") && !x.StartsWith("http") ? x[1..] : x);
return string.Join("/", prefixAdjusted);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment