I want to read stream in parallel from FTP server but i getting only two streams read at a time
Parallel.For(0, dt.Rows.Count, i =>
{
downloadFile();
});
public void downloadFile(long StartPoint, long EndPoint, string FtpAddr, string DfileName, int i)
{
string FTPAddress = @"ftp:\\" + FtpAddr;
string filename = DfileName;
string Partfilename = Path.GetFileName(DfileName);
long length = EndPoint - StartPoint;
try{
Stream strLocal;
WebProxy wp = new WebProxy();
wp.UseDefaultCredentials = true;
FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
request.Proxy = wp;
request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Proxy = wp;
request.UsePassive = false;
request.UseBinary = true;
request.KeepAlive = true;
request.ContentOffset = StartPoint;
Stream reader = request.GetResponse().GetResponseStream();
byte[] buffer = new byte[1048576];
if (!File.Exists(filename))
{
strLocal = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
else
{
strLocal = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
}
int bytesSize = 0;
while ((bytesSize = reader.Read(buffer, 0, buffer.Length)) > 0)
{
if ((strLocal.Length + bytesSize) < length)
{
strLocal.Write(buffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
}
else
{
long Valtowrite = (strLocal.Length + bytesSize) - length;
Valtowrite = bytesSize - Valtowrite;
strLocal.Write(buffer, 0, (int)Valtowrite);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, length, i, Partfilename, bytesSize });
break;
}
}
strLocal.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "There was an error connecting to the FTP Server.");
}
}
please help me its urgent
Thanks in Advance