C # Ftp download program source code parsing

Source: Internet
Author: User

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 Operations
{
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); // It can be the absolute or relative path of the ftp server
// URI can be relative or absolute. If the URI format is "ftp://contoso.com/%2fpath" (% 2f is the Escape Character "/"), the URI is absolute and the current directory is/path. However, if the URI format is "ftp://contoso.com/path", first. NET Framework logs on to the FTP server (using the username and password set by the Credentials property) and then sets the current directory /Path.
// Uri is not a url. Therefore, you must enter ftp: // url/cftea.txt In the first text box.
FtpRequest. Credentials = new NetworkCredential (textBox2.Text, textBox3.Text );
// You must have a valid user name and password for the server, or the server must allow anonymous logon. You can set the Credentials attribute to specify the Credentials used to connect to the server, or include them in the UserInfo section of the URI passed to the Create method. If the URI contains UserInfo information, use the specified username and password to set the Credentials attribute to a new network credential.
// Provide creden。 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 Socket Layer (SSL) client authentication.
// Public NetworkCredential (string userName, string password)

FtpWebResponse ftpResponse = (FtpWebResponse) ftpRequest. GetResponse ();
// To access FTP-specific attributes, you must forcibly convert the WebResponse object returned by this method to FtpWebResponse.
// Return the FTP server response.
// FtpWebResponse encapsulate the response of the FTP server to the request.


Stream data = ftpResponse. GetResponseStream (); // obtain the response Stream through the response object
// Get the stream, which is used to read the response body from the server
// This program retrieves the stream containing the response data sent from the FTP server
// The Return Value of the get method is a Stream that contains the response body.
// The GetResponseStream method returns data streams from the requested Internet resource.
String str = textBox1.Text. Substring (textBox1.Text. LastIndexOf ("/"), textBox1.Text. Length-textBox1.Text. LastIndexOf ("/"));
// If this character is found at the beginning of startIndex and the substring of function parameter 1 and parameter 2 is equivalent to the length, it is the index position of value starting from scratch; -1 if not found.
// Str. length specifies the number of characters in the current string.
// The report specifies the index position starting from scratch for the last match of Unicode characters in this instance. (In this example, it is the subscript position of the last)
// The final extracted string is textBox1.Text removed/The previous character
String SavePath = str;
If (File. Exists (SavePath ))
{
File. Delete (str );
}
Byte [] buffer = new byte [4096];
FileStream stream = new FileStream (SavePath, FileMode. Create );
// Use the specified path and Creation Mode to initialize a new instance of the FileStream class. Supported by. NET Compact Framework.
// Summary:
// Create a new file for the specified operating system. If the file already exists, it will be overwritten. This requires System. Security. Permissions. FileIOPermissionAccess. Write
// Permission. FileMode. Create is equivalent to the following request: if the file does not exist, use System. IO. FileMode. CreateNew; otherwise, use
// System. IO. FileMode. Truncate. If the file already exists but is hidden, the System. UnauthorizedAccessException exception is thrown.
Int count = 0;
Do
{
Count = data. Read (buffer, 0, buffer. Length); // Read the data stream object of the response obtained from ftp
// Public abstract int Read (byte [] buffer, int offset, int count) the first parameter is the second byte. The second parameter is the offset. The third parameter is the number of characters Read.
If (count> 0)
{
Stream. Write (buffer, 0, count); // read
// This is the file stream object. It reads characters from the Data Stream Obtained from ftp and writes them to the file stream for saving.
}
} While (count> 0 );
// Read the total number of bytes in the buffer. If the number of available bytes is less than the number of requested bytes,
// The total number of bytes may be smaller than the number of bytes requested, or if it has reached the end of the stream, it will be zero (0)
FtpResponse. Close (); // the previous sentence is intended to read the string. This sentence is disabled by the FtpWebResponse object stream.
Stream. Close (); // Close stream
}


}
}

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.