C # implement ftp Functions

Source: Internet
Author: User
String ftpServerIP = "apssdb ";
String ftpUserID = "ebideai ";
String ftpPassword = "ebideai ";

Private void Upload (string filename)
{
FileInfo fileInf = new FileInfo (filename );
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
FtpWebRequest reqFTP;

ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri ("ftp: //" + ftpServerIP + "/" + fileInf. Name ));

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 ();

MessageBox. Show ("success", "Upload Error ");
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message, "Upload Error ");
}
}

Private void Download (string filePath, string fileName)
{
FtpWebRequest reqFTP;

Try
{
FileStream outputStream = new FileStream (filePath + "\" + fileName, FileMode. Create );

ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri ("ftp: //" + ftpServerIP + "/" + fileName ));

ReqFTP. Method = WebRequestMethods. Ftp. DownloadFile;

ReqFTP. UseBinary = true;

ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );

FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();

Stream ftpStream = response. GetResponseStream ();

Long cl = response. ContentLength;

Int buffer size = 2048;

Int readCount;

Byte [] buffer = new byte [bufferSize];

ReadCount = ftpStream. Read (buffer, 0, bufferSize );

While (readCount> 0)
{
OutputStream. Write (buffer, 0, readCount );

ReadCount = ftpStream. Read (buffer, 0, bufferSize );
}

FtpStream. Close ();

OutputStream. Close ();

Response. Close ();
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}

Private void button3_Click (object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog ();
Op. RestoreDirectory = true;
If (DialogResult. OK = op. ShowDialog ())
{
TextBox1.Text = op. FileName;
}

// Upload (textBox1.Text );
Upload (textBox1.Text );


}

Private void button4_Click (object sender, EventArgs e)
{
Download (@ "d:", "table 092.161. SQL ");
}

Http://www.cnblogs.com/kerryking/archive/2007/07/10/813079.aspx

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.