About C # connection FTP upload download Implementation principle and code _ Practical skills

Source: Internet
Author: User
Tags readline
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.IO;
Namespace Ftponload
{
Class Program
{
static void Main (string[] args)
{
Ways to upload files
OnLoad ("D://output.txt");
Ways to download files
Fload ();
}
public static void onload (string file)
{
To construct a request object for a Web server
FtpWebRequest FTP;
Instantiating a File object
FileInfo f = new FileInfo (file);
FTP = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://192.168.0.150/" + f.name));
Create User name and password
Ftp.credentials = new NetworkCredential ("123", "123");
Ftp.keepalive = false;
Ftp.method = WebRequestMethods.Ftp.UploadFile;
Ftp.usebinary = true;
Ftp.contentlength = F.length;
int bufflength = 20480;
byte[] buff = new Byte[bufflength];
int Contentlen;
Try
{
Gets the input stream of the request object
FileStream fs = F.openread ();
Stream SW = Ftp.getrequeststream ();
Contentlen = fs. Read (Buff, 0, bufflength);
while (Contentlen!= 0)
{
Sw. Write (Buff, 0, Contentlen);
Contentlen = fs. Read (Buff, 0, bufflength);
}
Sw. Close ();
Fs. Close ();
}
catch (Exception e)
{
Console.WriteLine (E.message);
}
}
public static void Fload ()
{
FtpWebRequest FTP;
FTP = (ftpwebrequest) ftpwebrequest.create (The New Uri ("ftp://192.168.0.6/connected to the file you specify");
Specify User name and password
Ftp.credentials = new NetworkCredential ("123", "123456");
WebResponse WR = Ftp.getresponse ();
StreamReader sr = new StreamReader (WR. GetResponseStream (), System.Text.Encoding.Default);
string s = Sr. ReadLine ();
while (S.equals (""))
{
s = Sr. ReadLine ();
}
}
}
}
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.