There are certain times when you want to download a file asynchronously yet without
showing a dialog window. This can be accomplished via the static method DownloadFilesHidden
method of the DownloadDialog class.

Below is a sample method demonstrating how this method could be used.

void SampleDownloadFileAsyncNoDialog(string fileName, Action completeAction)
{
    try
    {
        var ext = Path.GetExtension(fileName);
        var saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "All files (*.*)|*.*|File (*" + ext + ")|*" + ext;
        saveFileDialog.FilterIndex = 2;
        if (saveFileDialog.ShowDialog() == true)
        {
            DownloadFileDialog.DownloadFilesHidden(
            // The file name the stream will be saved to
            saveFileDialog.SafeFileName
            // The stream to save to
            , saveFileDialog.OpenFile()
            // Callback when download completes
            , (canceled, error, stream) =>
            {
                try
                {
                    if (null != error)
                    {
                        MessageBox.Show(error.Message);
                    }
                    else if (canceled)
                    {
                        MessageBox.Show("Download was Canceled.");
                    }
                    else
                    {
                        // Do something useful with the downloaded stream...
                        // The stream is the same stream you provided above, but now contains the contents of the downloaded file
                    }
                }
                finally
                {
                    // Must explicitly close the stream.
                    if (null != stream)
                        stream.Close();
                    // Call the Complete Action
                    completeAction();
                }
            }
            // provide the file name to download
            , fileName);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
        Debug.WriteLine(ex.ToString());
    }
}

Last edited Jul 24, 2011 at 5:09 PM by hyspdrt, version 2

Comments

codeguyross Jun 7, 2013 at 8:42 PM 
This example provides a function but no information on how to call the function. For example, what is a completeAction?