Скачивание:Когда нажимаю Кнопку Download Form1 перестает отвечать после того как файл скачался Form1 начинает работать как это исправить.
Как сделать так чтобы Form1 не зависал а заднем плане скачивался файл?
private void Download_Click(object sender, EventArgs e)
{
FTPDownload(appPath, "file.7z", "xxxx xxx.com", "username", "pass");
}
public Boolean FTPDownload(string filePath, string fileName, string FTPAddress, string username, string password)
{
FtpWebRequest requestFTP;
try
{
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
requestFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPAddress + "/" + fileName));
requestFTP.Method = WebRequestMethods.Ftp.DownloadFile;
requestFTP.UseBinary = true;
requestFTP.Credentials = new NetworkCredential(username, password);
FtpWebResponse response = (FtpWebResponse)requestFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 1048;
int readCount;
byte[] buffer = new byte[bufferSize];
int totalReadBytesCount = 0;
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
label1.Text = readCount.ToString();
}
ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception)
{
return false;
}
}