I've looked it up on google, tried any answers for myself and none worked. I want to download 2 files and save them both to the users temp file (C:\Users%UserName%\AppData\Local\Temp). I can easily find the temp file using a string (string tempPath = Environment.GetEnvironmentVariable("TEMP");) I just can't get farther than that at this time.
CodePudding user response:
You can download directly into the temp folder:
using System.Net;
string tempPath = Environment.GetEnvironmentVariable("TEMP");
using (var client = new WebClient())
{
client.DownloadFile("http://example.com/file/file1.txt", @$"{tempPath}\file.txt");
client.DownloadFile("http://example.com/file/file2.txt", @$"{tempPath}\file2.txt");
}
Or move the already downloaded file:
using System.IO;
string tempPath = Environment.GetEnvironmentVariable("TEMP");
File.Move(@"C:\User\Downloads\Filename.txt", @$"{tempPath}\file.txt");
File.Move(@"C:\User\Downloads\Filename2.txt", @$"{tempPath}\file2.txt");
The @
before the string is the string literal and The $
is the string interpolation you can search for these nice features later.
CodePudding user response:
Adding to the accepted answer:
you can also derive the download-folder path to work on machine which use a different download folder as the default. You can find input on that here: How to programmatically derive Windows Downloads folder "%USERPROFILE%/Downloads"?
So if you chose to implement the second solution (moving files already downloaded) and need to roll this out on other machines than your own, this probably is a good idea.