2014-07-16 91 views
0

我想使用C#从FTP服务器下载许多(千)更小的文件。使用我当前的代码,我无法达到超过100 KB /秒的速度(通常慢得多)(我正在本地FileZilla FTP服务器上测试)。从FTP服务器快速下载多个文件

这是我的代码:

foreach (var file in files) 
{ 
    //Client is basically a WebClient 
    var stream = Client.OpenRead(new Uri(_serverRootPath + file.Replace(@"\", "/"))); 

    var filePath = _clientRootPath + file; 
    if (!Directory.Exists(Path.GetDirectoryName(filePath))) 
     Directory.CreateDirectory(Path.GetDirectoryName(filePath)); 
    var fileStream = new FileStream(_clientRootPath + file, FileMode.Create); 

    const int bufferSize = 8192; 
    var buffer = new byte[bufferSize]; 
    var readCount = stream.Read(new byte[bufferSize], 0, bufferSize); 

    while (readCount > 0) 
    { 
    await fileStream.WriteAsync(buffer, 0, readCount); 
    readCount = await stream.ReadAsync(buffer, 0, bufferSize); 
    } 

    stream.Close(); 
} 

任何帮助,将不胜感激。

+0

网络速度与仅下载1个大文件时相比如何? – Vlad

+0

@Vlad一个13MB的文件在不到一秒钟内就被转移了。 – davidwroxy

回答

相关问题