Louay Sleman
Louay Sleman

Reputation: 2106

How to create a onedrive file downloader in c#

I try to build an app store but I have a problem with my downloader I try to create a file downloader but it's not working at all !!! And my visual still tell me that my app doesn't have any errors on codes ! O think that the problem with the direct link from OneDrive ! Plz help me the code is :

[C#]

private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
 webClient.DownloadFileAsync(new Uri(url.Text), path.Text ; )

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
  progressBar.Value = e.ProgressPercentage;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}

Upvotes: 1

Views: 2736

Answers (1)

daniel59
daniel59

Reputation: 916

I guess your url is wrong. If you share a link to your file it looks like:

https://onedrive.live.com/redir?resid=698A32FCADE8DFDA%2121825

You have to replace the redir by download and it will download the file to your storage location:

string path = @"your storage location";
string source = "https://onedrive.live.com/download?resid=698A32FCADE8DFDA%2121825";//right download url
//string source = "https://onedrive.live.com/redir?resid=698A32FCADE8DFDA%2121825";//wrong download url

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadFileAsync(new Uri(source), path);

As an alternative you can simply open this link in your browser and the file will be automatically downloaded to your download directory:

Process.Start("https://onedrive.live.com/download?resid=698A32FCADE8DFDA%2121825");  

Upvotes: 4

Related Questions