'WebClient throw 'An exception occurred during WebClient request'
I know that question has been ask a lot in the internet, but yet i didn't found a satisfying answer.
private string LocalSqlDriverDownloader()
{
ProgBar prograssBar = new();
string sqlLocalDBUrl = "https://download.microsoft.com/download/7/c/1/7c14e92e-bdcb-4f89-b7cf-93543e7112d1/SqlLocalDB.msi";
string fileName = "SqlLocalDB.msi";
string directory = $@"{Path.GetPathRoot(Environment.SystemDirectory)}Download"; // C:\Download
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using WebClient webClient = new();
webClient.DownloadProgressChanged += (s, e) =>
{
Application.Current.Dispatcher?.Invoke(() =>
{
(prograssBar.DataContext as PrograssbarWindowViewModel).PrograssBarValue = e.ProgressPercentage;
});
};
webClient.DownloadFileCompleted += (s, e) =>
{
prograssBar.Close();
};
string downloadPath = $@"{directory}\{fileName}";
try
{
webClient.DownloadFile(sqlLocalDBUrl, downloadPath);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
prograssBar.ShowDialog();
return directory;
}
I don't have a clue why this throw to me an exception, I tried to download other files, http and https, it doesn't seams to have any difference to the outcome.
The given exception:
System.Exception
HResult=0x80131500
Message=An exception occurred during WebClient request.
Source=PulserTesterMultipleHeads
StackTrace:
at PulserTesterMultipleHeads.Models.MainTestPageMV.LocalSqlDriverDownloader() in C:\Project\Models\MainTestPageMV.cs:line 955
at PulserTesterMultipleHeads.Models.MainTestPageMV.LocalSQLDriverInstaller() in C:\Project\Models\MainTestPageMV.cs:line 905
at PulserTesterMultipleHeads.Models.MainTestPageMV..ctor(Action closeAction, String catalogDesc) in C:\Project\Models\MainTestPageMV.cs:line 70
at PulserTesterMultipleHeads.UserControls.MainTestPage..ctor() in C:\Project\UserControls\MainTestPage.xaml.cs:line 31
Solution 1:[1]
Remove this whole construct:
try
{
webClient.DownloadFile(sqlLocalDBUrl, downloadPath);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
and replace it by
webClient.DownloadFile(sqlLocalDBUrl, downloadPath);
Then you will still get an error, but at least you will be able to see what is wrong and where. The exception and maybe the inner exception will tell you in no uncertain terms what is wrong. The stack trace will tell you where it went wrong.
As it is, you have added this block and all it does is it removes the information you need to find out what is wrong. We cannot tell you what went wrong specifically, because your code intentionally removes it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nvoigt |