public static async Task SendFileAsync( this HttpListenerResponse response, FileInfo file) { if (file is null) { throw new ArgumentNullException(nameof(file)); } if (!file.Exists) { throw new FileNotFoundException("File not found: " + file.FullName, file.FullName); } MediaType type = MediaType.Application_Octet_Stream; var types = MediaType.GuessByFile(file); if (types.Count > 0) { type = types[0]; } response.SetFileName(type, file.Name); using var input = file.OpenRead(); await response.SendStreamAsync(type, input) .ConfigureAwait(false); }