开发者

Can i Use FTP for accessing remote Folder and Files

I have set of excel files in folder in remote server. And one WPF application is running on my local machine. Now i want to able to look at files in the remote folder and downloa开发者_开发百科d. How to achieve this?

Can i Use FTP for this? If Possible How?

Thanks in advance. :)


It is possible. Here's a nice example. And msdn documentation.

FileStream outputStream = new FileStream(destinationoffile, FileMode.Create);
FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse response = request.GetResponse() as FtpWebResponse;
Stream responseStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
float perc = 0;
float result = 0;
float totalread = 0;
byte[] buffer = new byte[bufferSize];
readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
         totalread += readCount;
         result = totalread / fileSize;
         perc = result * 100;
         progress.Value = perc; // this is a progressbar on my screen so may show error for you
         outputStream.Write(buffer, 0, readCount);
         readCount = responseStream.Read(buffer, 0, bufferSize);
}

responseStream.Close();
response.Close();
outputStream.Close(); // keeps file open if not closed
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜