簡體   English   中英

無法在WPF C#中將下載的文件保存到桌面

[英]Trouble saving a downloaded file to the desktop in WPF C#

我用的時候:

  WebClient web = new WebClient();
  web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChangedWeb);
  web.DownloadFileAsync(new Uri("http://www.website.com/Webs.exe"),
                            Environment.SpecialFolder.Desktop + @"\Webs.exe");

......沒有下載。

但如果我改為“

  WebClient web = new WebClient();
  web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChangedWeb);
  web.DownloadFileAsync(new Uri("http://www.website.com/Webs.exe"),
                            Environment.SpecialFolder.Desktop + "Webs.exe");

然后它下載,但我得到一個名為“desktopWebs.exe”的文件。 那么如何將文件保存到桌面?

謝謝

你想要的是......

Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Webs.exe";

否則你只是在單詞desktop而不是實際路徑上。

您可以使用Path.Combine

web.DownloadFileAsync(new Uri("http://www.website.com/Webs.exe"),
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Webs.exe"));

此功能將自動插入(或刪除)斜杠,以及適應所使用的任何文件系統

您還應該考慮使用Environment.SpecialFolder.DesktopDirectory,它指向磁盤上桌面文件夾的實際物理位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM