Use. net FtpWebRequest to implement common FTP Functions

Source: Internet
Author: User
Tags ftp connection

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Net;
Using System. Windows. Forms;
Using System. Globalization;

Namespace FtpTest1
{

Public class FtpWeb
{
String ftpServerIP;
String ftpRemotePath;
String ftpUserID;
String ftpPassword;
String ftpURI;

/// <Summary>
/// Connect to FTP
/// </Summary>
/// <Param name = "FtpServerIP"> FTP connection address </param>
/// <Param name = "FtpRemotePath"> specifies the current directory after successful FTP connection. If this parameter is not specified, it is the root directory by default. </param>
/// <Param name = "FtpUserID"> User name </param>
/// <Param name = "FtpPassword"> password </param>
Public FtpWeb (string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
FtpServerIP = FtpServerIP;
FtpRemotePath = FtpRemotePath;
FtpUserID = FtpUserID;
FtpPassword = FtpPassword;
FtpURI = "ftp: //" + ftpServerIP + "/" + ftpRemotePath + "/";
}

/// <Summary>
/// Upload
/// </Summary>
/// <Param name = "filename"> </param>
Public void Upload (string filename)
{
FileInfo fileInf = new FileInfo (filename );
String uri = ftpURI + fileInf. Name;
FtpWebRequest reqFTP;

ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (uri ));
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
ReqFTP. KeepAlive = false;
ReqFTP. Method = WebRequestMethods. Ftp. UploadFile;
ReqFTP. UseBinary = true;
ReqFTP. ContentLength = fileInf. Length;
Int buffLength = 2048;
Byte [] buff = new byte [buffLength];
Int contentLen;
FileStream fs = fileInf. OpenRead ();
Try
{
Stream strm = reqFTP. GetRequestStream ();
ContentLen = fs. Read (buff, 0, buffLength );
While (contentLen! = 0)
{
Strm. Write (buff, 0, contentLen );
ContentLen = fs. Read (buff, 0, buffLength );
}
Strm. Close ();
Fs. Close ();
}
Catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert ("FtpWeb", "Upload Error -->" + ex. Message );
}
}

/// <Summary>
/// Download
/// </Summary>
/// <Param name = "filePath"> </param>
/// <Param name = "fileName"> </param>
Public void Download (string filePath, string fileName)
{
FtpWebRequest reqFTP;
Try
{
FileStream outputStream = new FileStream (filePath + "\" + fileName, FileMode. Create );

ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (ftpURI + fileName ));
ReqFTP. Method = WebRequestMethods. Ftp. DownloadFile;
ReqFTP. UseBinary = true;
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Stream ftpStream = response. GetResponseStream ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.