The previous introduction of the FTP file operation, and today we will introduce a file to get a list of functions. This function should be the simplest one, it just gets the file information, but not the actual data transfer.
Here is the core code for this feature:
/// <summary> ///get a list of files/// </summary> /// <param name= "Ftpserverip" >Server Address</param> /// <param name= "Ftpuserid" >FTP user name</param> /// <param name= "FTPPassword" >ftp Password</param> /// <returns></returns> Public string[] Getfilelist (stringFtpserverip,stringFtpuserid,stringFTPPassword) { string[] downloadfiles; StringBuilder result=NewStringBuilder (); FtpWebRequest reqftp; Try { //to create a FtpWebRequest object from a URIReqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (Ftpserverip)); //specifying data Transfer TypesReqftp.usebinary =true; //FTP user name and passwordReqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword); //specify what commands to executeReqftp.method =WebRequestMethods.Ftp.ListDirectory; WebResponse response=Reqftp.getresponse (); //Get file StreamStreamReader reader =NewStreamReader (response. GetResponseStream ()); stringline =Reader. ReadLine (); //add file names to file list if you have files while(Line! =NULL) {result. Append (line); Result. Append ("\ n"); Line=Reader. ReadLine (); } result. Remove (result. ToString (). LastIndexOf ('\ n'),1); //Close the streamReader. Close (); Response. Close (); returnResult. ToString (). Split ('\ n'); } Catch(Exception ex) {downloadfiles=NULL; returnDownloadfiles; } }
About the FTP article wrote a few, C # FTP Basic operation is written almost. This blog post should be the last article on C#+ftp.
List of Get files for FTP file operation