Skip to content

Instantly share code, notes, and snippets.

@psvramaraju
Created July 11, 2017 11:32
Show Gist options
  • Save psvramaraju/f58037c067e039e2f780a529b47b14e9 to your computer and use it in GitHub Desktop.
Save psvramaraju/f58037c067e039e2f780a529b47b14e9 to your computer and use it in GitHub Desktop.
Downloading file to a custom folder in wpf c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Download
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "myimage"; // Default file name
dlg.DefaultExt = ".png"; // Default file extension
dlg.Filter = "Image file (*.png) | *.png"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("https://assets.onestore.ms/cdnfiles/external/uhf/long/9a49a7e9d8e881327e81b9eb43dabc01de70a9bb/images/microsoft-gray.png"),
dlg.FileName);
// Save document
string filename = dlg.FileName;
}
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment