示例#1
0
        public static bool DownloadFile(string fileName)
        {
            var request =
                (FtpWebRequest)WebRequest.Create(String.Format("ftp://{0}/{1}", Settings.Default.FTP_Server, fileName));

            request.Method      = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(Settings.Default.FTP_UserID, Settings.Default.FTP_Password);

            FtpWebResponse response = null;

            try
            {
                response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException)
            {
                MessageBox.Show("Kon het bestand niet ophalen van de server. Mogelijk bestaat het bestand niet.",
                                "Bestand bestaat niet", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }

            Stream responseStream = response.GetResponseStream();

            Task <bool> openFileTask = TaskExt.StartSTATask(() =>
            {
                using (var fbd = new FolderBrowserDialog())
                {
                    DialogResult result = fbd.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        File.WriteAllBytes(String.Format("{0}\\{1}", fbd.SelectedPath, Path.GetFileName(fileName)),
                                           responseStream.ReadAllBytes());
                        Debug.WriteLine("Download Complete, status {0}", response.StatusDescription);
                        response.Close();
                        return(true);
                    }
                }
                return(false);
            });

            openFileTask.Wait();

            response.Close();
            return(openFileTask.Result);
        }