There is a list with links to download files (for example, https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-2002.xml.zip ). I need to download all the files from this list to a specific folder. How to do it on C # Core? I believe that you need to use HttpClient () instead of the previously used WebClient ().

On older versions of C #, you could do this:

var сlient = new WebClient(); string path = "some/path/to/download"; webClient.DownloadFile("https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-2002.xml.zip", path); 

How to perform download in C # Core? It does not support the WebClient () class.

  • Give an example of how you are trying to do this and what exactly you have a problem with! - Yury Bakharev
  • @Yury Bakharev thanks, I already solved the problem - Viktor Bylbas

1 answer 1

Solved this issue:

 public static async void DownloadFile(string url, string path) { byte[] data; using (var client = new HttpClient()) using (HttpResponseMessage response = await client.GetAsync(url)) using (HttpContent content = response.Content) { data = await content.ReadAsByteArrayAsync(); using (FileStream file = File.Create(path)) //path = "wwwroot\\XML\\1.zip" file.Write(data, 0, data.Length); } } 
  • Async void is for events only. In your case, the caller has nothing to expect (void) because there is no Task object. - Vladislav Khapin
  • @VladislavKhapin, say it right, but I had to at least have the files download. I managed to download files and I immediately gave an example, and if my example would be useful to someone, I believe that they will be able to complete the exception handling and return what they need - Viktor Bylbas