C # FTP Download program source code parsing

Source: Internet
Author: User
Tags file transfer protocol

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.IO;
Using System.Net;
Using System.Threading.Tasks;
Using System.Windows.Forms;


Namespace Network operation
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}


private void Menustrip2_itemclicked (object sender, ToolStripItemClickedEventArgs e)
{


}


private void Button1_click_1 (object sender, EventArgs e)
{
FtpWebRequest ftprequest = (ftpwebrequest) webrequest.create (TextBox1.Text);//Can be the absolute path of ftpserver can also be a relative path
The URI can be relative or absolute. Assuming that the URI is in the form "Ftp://contoso.com/%2fpath" (%2f is the escape character "/"), the URI is absolute and the current folder is/path. However, assuming that the URI is in the form "Ftp://contoso.com/path", first the. NET Framework is logged on to the FTP server (using the username and password set by the Credentials property), Then set the current folder to <userlogindirectory>/path.
URI is not a URL so the first text box should be entered Ftp://url/cftea.txt
Ftprequest.credentials = new NetworkCredential (TextBox2.Text, TextBox3.Text);
You must have a valid username and password for the server, or the server must agree to log on anonymously. The ability to specify the credentials used to connect to the server by setting the Credentials property can also be included in the UserInfo portion of the URI passed to the Create method. Assuming that the URI includes UserInfo information, the Credentials property is set to the new network credential using the specified username and password information.
Provides credentials for password-based authentication schemes such as basic, brief, NTLM, and Kerberos authentication.
This class does not support public key-based authentication methods, such as Secure Sockets Layer (SSL) client Authentication
Public networkcredential (string username,string password)

FtpWebResponse ftpresponse = (ftpwebresponse) ftprequest.getresponse ();
To access FTP-specific properties, you must cast the WebResponse object returned by this method to FtpWebResponse.
Returns the FTP server response.
FtpWebResponse encapsulates the response of a file Transfer Protocol (FTP) server to a request.


Stream data = Ftpresponse.getresponsestream (); Get a response stream from a response object
Gets the stream that is used to read the body of the response from the server
This program retrieves the stream that includes the response data sent from the FTP server
The return value of the Get method is a Stream that includes the body of the response.
The GetResponseStream method returns the data stream from the requested Internet resource.
String str = textBox1.Text.Substring (TextBox1.Text.LastIndexOf ("/"), TextBox1.Text.Length- TextBox1.Text.LastIndexOf ("/"));
A string of function parameters 1 and 2 that begins at StartIndex and length is the equivalent of a substring that is equal to the zero-based index position of value if the character is found, or 1 if it is not found.
Str.length the number of characters in the current string.
Reports the zero-based index position of the last occurrence of the specified Unicode character in this instance. (In this case, the last/subscript position)
The last extracted string is TextBox1.Text removed/preceded by the character
string savepath = str;
if (file.exists (Savepath))
{
File.delete (str);
}
byte[] buffer = new byte[4096];
FileStream stream = new FileStream (Savepath, FileMode.Create);
Initializes a new instance of the FileStream class with the specified path and creation mode. Supported by the. NET Compact Framework.
Summary:
Specifies that the operating system should create a new file. Assuming the file already exists, it will be overwritten. It needs System.Security.Permissions.FileIOPermissionAccess.Write.
Permissions. FileMode.Create is equivalent to this request: If the file does not exist, the System.IO.FileMode.CreateNew is used;
System.IO.FileMode.Truncate. The system.unauthorizedaccessexception exception is thrown if the file already exists but is hidden.
int count = 0;
Do
{
Count = data. Read (buffer, 0, buffer. Length); Read the data flow object for the response obtained from FTP
public abstract int Read (byte[] buffer,int offset,int count) The first parameter is a byte the second is the offset the third number of characters to read
if (Count > 0)
{
Stream. Write (buffer, 0, count);//participation Read
This is a file stream object that reads the characters from the data stream obtained from FTP and then writes the file stream to save
}
} while (Count > 0);
The total number of bytes read into the buffer. Assuming that the number of bytes currently available is not as many as the number of bytes requested,
The total number of bytes may be less than the number of bytes requested, or 0 (0) If the end of the stream has been reached
Ftpresponse.close ();//The last sentence is intended to read the string, this sentence is the FtpWebResponse object flow closed
Stream.   Close (); Stream flow off
}


}
}

C # FTP Downloader source code parsing

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.