JimDel
JimDel

Reputation: 4359

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

When I use:

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

...Nothing downloads.

But if i change it to"

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

Then It downloads, but I get a file named "desktopWebs.exe". So How can I save a file to the desktop?

Thanks

Upvotes: 3

Views: 1874

Answers (2)

jimmyjambles
jimmyjambles

Reputation: 1670

You can use Path.Combine

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

This function will automatically insert (or remove) slashes as well as adapt to any file system being used

You should also consider using Environment.SpecialFolder.DesktopDirectory, this points to the actual physical location of the desktop folder on the disk.

Upvotes: 3

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

What you want is this...

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

Otherwise you are just tacking on the word desktop instead of the actual path.

Upvotes: 5

Related Questions