Home > other >  How do you write program to connect FTP
How do you write program to connect FTP

Time:09-17

Program to connect FTP how to write, what a master of caring the great spirit level, to provide a train of thought or ready-made code

CodePudding user response:

A written in c # code, you can see if


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
Using System.Net;
Using System.Net.FtpClient;
using System.Text;
Using System. The Threading. The Tasks;
Using System. IO;

The namespace FTP_Client
{
Public class FTPConnection
{

Public FTPConnection () {}

///& lt; Summary>
///connect to FTP server function
///& lt;/summary>
///& lt; Param name="strServer & gt;" Server IP
///& lt; Param name="strUser & gt;" The user name & lt;/param>
///& lt; Param name="strPassword" & gt; Password & lt;/param>
Public bool FTPIsConnected (string strServer, string strUser, string strPassword)
{
Using (FtpClient FTP=new FtpClient ())
{
FTP. Host=strServer;
FTP. Credentials=new NetworkCredential (strUser, strPassword);
FTP. The Connect ();
Return the FTP. IsConnected;
}
}


///& lt; Summary>
///FTP download file
///& lt;/summary>
///& lt; Param name="strServer & gt;" Server IP
///& lt; Param name="strUser & gt;" The user name & lt;/param>
///& lt; Param name="strPassword" & gt; Password & lt;/param>
///& lt; Param name="Serverpath & gt;" The server path, example: "/Serverpath/" & lt;/param>
///& lt; Param name="localpath & gt;" Local save the path & lt;/param>
///& lt; Param name="filetype & gt;" Download the file type, example: "rte" & lt;/param>
Public bool FTPIsdownload (string strServer, string strUser, string strPassword, string Serverpath, string localpath, string filetype)
{

FtpClient FTP=new FtpClient ();
FTP. Host=strServer;
FTP. Credentials=new NetworkCredential (strUser, strPassword);
FTP. The Connect ();

String path=Serverpath;
String destinationDirectory=localpath;
List Documentname=new List (a);
Bool DownloadStatus=false;

If (Directory. The Exists (destinationDirectory))
{
# region from the FTP server to download file
Foreach (var ftpListItem in FTP. GetListing (path, FtpListOption. Modify | FtpListOption. Size)
The Where (ftpListItem=& gt; String. The Equals (Path. GetExtension (ftpListItem. Name), filetype)))
{
String destinationPath=the string. Format (@ "{0} \ {1}", destinationDirectory, ftpListItem. Name);
Using (Stream ftpStream=FTP. OpenRead (ftpListItem. FullName))
Using (FileStream FileStream=File. The Create (destinationPath, (int) ftpStream. Length))
{
Var buffer=new byte (200 * 1024),
Int count;
While ((count=ftpStream. Read (buffer, 0, buffer Length)) & gt; 0)
{
FileStream. Write (buffer, 0, count);
}
}
Documentname. Add (ftpListItem. Name);
}
# endregion

# region to verify whether there are local the file
String [] files=Directory. GetFiles + filetype (localpath, "*");
Int filenumber=0;
The foreach (string strfilename in files)
{
The foreach (string strrecievefile in documentname)
{
If (strrecievefile==Path. GetFileName (strfilename))
{
Filenumber++;
break;
}
}
}
If (filenumber==documentname. Count)
{
DownloadStatus=true;
}
# endregion
}
Return DownloadStatus;
}

}
}

CodePudding user response:

Thank you, god's help
  • Related