65.9K
CodeProject 正在变化。 阅读更多。
Home

简单 FTP 操作...

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.31/5 (11投票s)

2007年3月15日

viewsIcon

41591

用 4-5 行代码实现 FTP 操作...

引言

本文将帮助您在 5 分钟内编写 FTP 操作...

我们知道 FTP 目录结构与我们系统上的目录结构相同,但区别在于 FTP 目录位于其他位置。

因此,当您上传文件时,您实际上是将一个文件从一个地方传输到另一个地方,就像在 Unix 编程中,如果我们想将一个文件从一个地方复制到另一个地方,我们

背景

有关本文中使用的类的更多信息,请参考 MSDN...

使用代码

您只需将此代码复制并粘贴到您的代码中,即可上传或下载您指定的 FTP 文件...
:

//

// Just Rename File name and FTP location that you want to upload or download.

// 

//Generate the File name which you want to upload on the FTP... 

string ftpFilename = ftpServer + FileName;

//Create a Object of class WebClient.. 

WebClient client = new WebClient();
//Give the Credincials which will help you to enter into the FTP..


client.Credentials = new NetworkCredential(loginName, ftpPwd);

//Now use .UploadFile Method of WebClient Object to upload the file,

this method will return response from the FTP server which you can use as, 
result of FTP operation.. 

responseArray = client.UploadFile(ftpFilename, filetobeUploaded); 
 
//here you have to pass full path of FTP directory with filename included as

 ftpFilename, like if you want to upload file to ftp://simpleftp.com then

use this kind of structure..
ftpFilename = "ftp://simpleftp.com/test.txt";
//and in the 'filetobeUploaded' give name of your file to be uploaded..

 
//same way you can download the file by using .downloadfile method of the 

Webclient class Object.
 
//so enjoy the FTP operation in just 5 minutes..


关注点

我喜欢与像您这样的人分享我的知识,因为我也是你们中的一员,如果我不知道任何事情,我会来找您...所以保持知识的交流...

历史

如果我找到更简短的 FTP 方法,我一定会将其添加到本文中...

© . All rights reserved.