2016-03-21 41 views
1

我嘗試構建應用程序商店,但我的下載程序出現問題我嘗試創建文件下載程序,但它根本不工作! 而我的視覺仍然告訴我,我的應用在代碼上沒有任何錯誤! O認爲與OneDrive的直接鏈接的問題! plz幫助我的代碼是:如何在c#中創建onedrive文件下載程序#

[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!"); 
} 
+1

你是否得到'NotWorkingAtAllException'或其他東西?看:[問]並請[參觀] – Plutonix

+0

我什麼也沒有得到,只是不工作! – louay

+0

@ louaysleman可能出現的問題示例:*「當我單擊按鈕時,什麼也沒有發生,並且沒有斷點。」 「到達這條線時,會拋出X類型的異常。」 「我的回調函數永遠不會被調用。」*只是說「它不工作」不能幫助我們或你。 – Katana314

回答

4

我猜你的網址是錯誤的。如果您共享一個鏈接到您的文件,它看起來像:

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

您必須更換redir通過download,它會下載文件到您的存儲位置:

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); 

作爲替代方案,您只需在瀏覽器中打開此鏈接,該文件將自動下載到您的下載目錄中:

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

我會試試這個!謝謝 – louay

+1

謝謝,現在正在工作:) – louay

相關問題