Use C # Builder implementation file download

zhaozj2021-02-08  217

Author: Xu Changyou a. Overview: This article introduces some of the basic knowledge of Internet communication programming with C # Builder through an example. We know that the .NET class contains a request / response layer, an application protocol layer, a transport layer, etc. In this program, we use the WebRequest class in the request / response layer and the WebClient class, etc. to achieve high abstract degree of Internet communication services. The function of this program is to complete the download. two. Realization Principle: The principle of program implementation is relatively simple, mainly used to use the WebClient class and the FileStream class. Where the WebClient class is in the System.Net namespace, the primary function of this class is to provide a public method for transmitting data to the URI identifier and resources from the URI identifier. We use the downfile () method to download the file to the local. The file data is then written to the local file in a data stream with an instance object of the FILESTREAM class. This completes the download of the file. three. Implementation steps: 1. First, open C # Builder, File-> New-> C # Application, Name We are set to "Download". 2. The settings for the main interface. TEXT is set to "File Download", and StartPosition is set to CENTERSCREEN, and the maximizebox is set to false. We add the following controls on the main form: two tag controls label1, label2, a text box control TextBox1, a button control Button1 and a progress bar Control Progressbar1.

Label1: Text is URL; label2: text is a download progress; TextBox1: text is set to empty; button1: text is set to download; 3. Program encoding // Process Downfile, download private void downfile () for completion files (String FileName; WebClient DownFile = new WebClient (); long fbytes; if (textBox1.Text = "!") {saveFileDialog1.ShowDialog (); FileName = saveFileDialog1.FileName; if (! FileName = "") {// get the file size WebRequest wr_request = WebRequest.Create (textBox1.Text); WebResponse wr_response = wr_request.GetResponse (); fbytes = wr_response.ContentLength; progressBar1.Maximum = (int) fbytes; progressBar1.Step = 1; wr_response.Close (); // download data starts DownFile.DownloadData (textBox1.Text); Stream strm = DownFile.OpenRead (textBox1.Text); StreamReader reader = new StreamReader (strm); byte [] mbyte = new byte [fbytes]; int allmybyte = (int) MByte.Length; Int startmbyte = 0; while (fbytes> 0) {INT m = Strm.read (mbyte, startmbyte, allmyby; if (m == 0) Break; startmbyte = m; allmybyte- = m; progressBar1.value = m;} FileStream fstrm = new FileStream (FileName, FileMode.OpenOrCreate, FileAccess.Write); fstrm.Write (mbyte, 0, startmbyte); strm.Close (); fstrm.Close ( ); Progravelbar1.value = progressbar1.maximum;}} else {messagebox.show ("No input to download)! ");}} // Double-click the" Download "button, enter the following code: Thread THREADSTART (NEWNSTART (Downfile)); th.Start (); // The complete code is as follows: use system; use system.drawing Using system.collections; using system.componentmodel; using system.windows.forms;

using System.Data; using System.Net; using System.IO; using System.Threading; namespace download {///

/// Summary description for WinForm /// public class WinForm: System.. Windows.Forms.Form {/// /// Required designer variable /// private System.ComponentModel.Container components = null;. private System.Windows.Forms.Label label1; private System.Windows .Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.SaveFileDialog saveFileDialog1; private System.Windows.Forms.Label label2; private System.Windows.Forms.ProgressBar progressBar1; public WinForm () {// Required for Windows Form Designer Support // InitializationComponent (); // // Todo: Add Any Construction Code After InitializationComponent Call / /} /// /// Clean Up Any Resources Being Used. / // Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) { Components.dispose ();}} Base.dispose (4);} #Region Windows Form Designer generated code /// /// Required method for Designer Support - Do Not Modify ///////////////////// The code editor. /// private () {this.label1 = new system.windows.forms.label (); this.textbox1 = new system.windows.Forms.TextBox (); this.button1 = new System.Windows.Forms.Button (); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog (); this.label2 = new System.Windows.Forms.Label (); this.progressBar1 = new System.Windows . Form.progressbar (); this.suspendlayout ();

/// Label1 // this.label1.Location = new system.drawing.point (40, 40); this.label1.name = "label1"; this.label1.size = new system.drawing.size (40, 16); this.label1.tabindex = 0; this.label1.text = "URL:"; // // textbox1 // this.textBox1.location = new system.drawing.point (72, 36); this.textBox1 .Name = "textBox1"; this.textbox1.size = new system.drawing.size (256, 21); this.textBox1.tabindex = 1; this.textBox1.text = ""; /// Button1 // this .Button1.Location = new system.drawing.point (256, 120); this.button1.name = "button1"; this.button1.tabindex = 2; this.button1.text = "Download"; this.button1.click = New system.eventhandler (this.button1_click); // // label2 // this.label2.Location = new system.drawing.point (80); this.label2.name = "label2"; this.Label2 .Size = new system.drawing.size (72, 23); this.label2.tabindex = 3; this.label2.text = "Download progress:"; /// progressbar1 // this.progressbar1.Location = new Syste M. Drawing.Point (72, 80); this.progressbar1.name = "progressbar1"; this.progressbar1.size = new system.drawing.size (256, 16); this.progressbar1.tabindex = 4; // / / Winform // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (360, 173); this.Controls.add (this.progressbar1); this.controls .Add (this.label2); this.controls.add (this.button1); this.controls.add (this.TextBox1); this.Controls.add (this.Label1); this.maximizebox = false; this.name = "Winform";

This.startPosition = system.windows.forms.formstartPosition.centerscreen; this.text = "File Download"; this.ResumeLayout (false);} #endregion ///

/// the main entry point for the application. /// [stathread] static void main () {Application.Run (new winform ());} privine filename; WebClient downfile = new WebClient (); long fbytes; if (TextBox1 ! .Text = "") {saveFileDialog1.ShowDialog (); FileName = saveFileDialog1.FileName;! if (FileName = "") {// get the file size WebRequest wr_request = WebRequest.Create (textBox1.Text); WebResponse wr_response = wr_request .GetResponse (); fbytes = wr_response.ContentLength; progressBar1.Maximum = (int) fbytes; progressBar1.Step = 1; wr_response.Close (); // start downloading data DownFile.DownloadData (textBox1.Text); Stream strm = DownFile .Openread (TextBox1.text); StreetReader Reader = new streamreader (strM); byte [] mbyte = new byte [fbytes]; int allmybyte = (int) mbyte.length; while (fbytes> 0) {INT m = Strm.read (mbyte, startmbyte, allmyby); if (m == 0) Break; startmbyte = m; allmybyte- = m ; progressBar1.value = m;} FileStream fstrm = new FileStream (FileName, FileMode.OpenOrCreate, FileAccess.Write); fstrm.Write (mbyte, 0, startmbyte); strm.Close (); fstrm.Close (); progressBar1. Value = progressbar1.maximum;}}} else {messagebox.show ("No input to download the file! ");}} Private void button1_click (object sender, system.eventargs e) {thread TH =

New Thread (New ThreadStart (Downfile)); th.Start ();}}} 4. Press F9 Run the program, enter a download address, and try the "Download" button to try. four. Conclusion: The above shows you how to use C # Builder to implement files with C # Builder, which is easy to find that Internet communication programming with C # Builder is very convenient. In the above program, we only use some methods of the WebClient class, and the WebClient class does not just provide the method of file download, but also provides file uploading and other methods, interested readers may try themselves, check help. Multi-threads are used in the program because the WebClient class has a large resource school, which will make the display of the entire window in complete. If you don't have multi-thread, you can not move the window or other operations at all. Interested friends can go to my homepage download source code

Http://yousoft.hi.com.cn

转载请注明原文地址:https://www.9cbs.com/read-2694.html

New Post(0)