2015-02-11 69 views
-3
string path = Path.GetDirectoryName(Application.ExecutablePath) + 
            "\\" + directores[directores.Length - 2] + 
            "\\" + Path.GetFileName(url_img).Replace(" ", "_"); 
client.DownloadFileAsync(new Uri(url_img), path); 

此代碼在不工作的路徑似乎是很好,當我通過MessageBox的檢查:C#DownloadFileAsync,不以絕對路徑工作

Image

我也曾嘗試與@符號,但它不起作用。

如果我輸入的不是路徑,但一個名字,e.g

client.DownloadFileAsync(new Uri(url_img), "test"); 

一切正常。我該如何解決這個問題?

忘了補充:如果這樣的路徑不存在,我想創建它!

+2

你是什麼意思的「不工作」。你有什麼異常? – 2015-02-11 09:02:48

+0

不,沒有例外,但它也不保存文件。 – Qentinios 2015-02-11 09:04:06

+1

嘗試其他路徑,即'c:\ test'; Offtopic:使用「Debug.Print」而不是MessageBox作爲診斷消息。 – i486 2015-02-11 09:15:24

回答

2

每當使用異步方法是您負責檢查結果。對於DownloadFileAsync,責任是執行DownloadFileCompleted。在這種情況下,看看Error,它會給你提供寶貴的線索爲什麼它失敗了。

-1

找到了解決辦法:

if (!Directory.Exists(directores[directores.Length - 2])) 
    Directory.CreateDirectory(directores[directores.Length - 2]); 



string path = Path.GetDirectoryName(Application.ExecutablePath) + 
           "\\" + directores[directores.Length - 2] + 
           "\\" + Path.GetFileName(url_img).Replace(" ", "_"); 
if (!Directory.Exists(directores[directores.Length - 2])) Directory.CreateDirectory(directores[directores.Length - 2]); 
client.DownloadFileAsync(new Uri(url_img), path); 

感謝試圖幫助。

所以這個函數不會創建文件夾,如果它不存在,我們必須先創建它。