Код:
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using System.IO;
using System.Text;
//Библиотеки для работы с MS SQL Server
using System.Data.SqlClient;
//Библиотеки для работы с XML
using System.Xml;
using System.Collections.Generic;
namespace WindowsFormsApplication4
{
public partial class Parent : Form
{
public Parent()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button2_Click);
listBox1.SelectionMode = SelectionMode.MultiExtended;
}
void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MessageBox.Show("Fin!");
}
string ftpAddr = File.ReadAllLines(@"1.txt")[4].Split(new Char [] { '<', ' ', '=', '>'}, StringSplitOptions.RemoveEmptyEntries)[1];
//string pass = "somePass";
//string login = "someLogin";
string outputFolder = @"d:\ftp";
string resFolder = @"D:\out";
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpAddr + "/"));
reqFTP.UseBinary = true;
//reqFTP.Credentials = new NetworkCredential(login, pass);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
string[] files = GetFileList();
foreach (string file in files)
{
listBox1.Items.Add(file);
}
}
private void button2_Click(object sender, EventArgs e)
{
var filesToDownload = listBox1.SelectedItems;
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
foreach (var file in filesToDownload)
{
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri("ftp://" + ftpAddr + "/" + file.ToString()), outputFolder +"\\"+ Path.GetFileName(file.ToString()));
}
}
Как сделать так, чтобы он не выводил после каждого загруженного файла сообщение "Fin", а только один раз при окончательной загрузке всех файлов и как сделать так чтобы при ошибках загрзки он выводил соответствующее сообщение?