Home > other >  c# - Migrate code from WebClient to HTTPClient
c# - Migrate code from WebClient to HTTPClient

Time:01-24

if (FileExtension == ".txt")
{
    string enumLines = File.ReadAllText(FoundPics, Encoding.UTF8);
    Clipboard.SetText(enumLines);
    using (WebClient client = new())
    {
        DirectoryInfo di = new DirectoryInfo(FoundPics);
        client.DownloadFile(enumLines, FolderPath   @"\"   "Template.gif");
        Image gifTemplate = Image.FromFile(FolderPath   @"\"   "Template.gif");
        pictureBox.Image = gifTemplate;
    }
}

I have this code that retrieves a url from a txt file, downloads a .gif from it and sets it to the picturebox However it's using Webclient that I was recommended to avoid. I'm trying to change to HTTPClient now but I'm really lost. Can someone provide some pseudo code so that I can figure out what to do?

CodePudding user response:

var data  = await client.GetByteArrayAsync(FolderPath   @"\"   "Template.gif");
File.WriteAllBytes(@"\path\to\downloads\Template.gif", data);
  1. FolderPath must be a uri
  2. Your application must have write access to the downloads folder
  • Related