File download function with Visual C #

xiaoxiao2021-03-06  100

Using system;

Using 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 mygetcar

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.Label label1;

Private system.windows.Forms.Label label2;

Private system.windows.Forms.TextBox Srcaddress;

Private system.windows.Forms.TextBox taraddress;

Private system.windows.Forms.statusbar statusbar;

Private system.windows.Forms.Button Start;

PRIVATE WebClient Client = New WebClient ();

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

THIS.LABEL1 = New System.windows.Forms.label ();

THIS.LABEL2 = New System.windows.Forms.label ();

THIS.SRCADDRESS = new system.windows.Forms.TextBox ();

This.taraddress = new system.windows.Forms.TextBox ();

This.statusbar = new system.windows.Forms.statusbar ();

This.start = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// label1

//

THIS.Label1.Location = new system.drawing.point (8, 32);

THIS.LABEL1.NAME = "label1";

THIS.Label1.size = new system.drawing.size (72, 23);

This.Label1.tabindex = 0; this.label1.text = "file address:";

THIS.Label1.TextAlign = system.drawing.contentAlignment.middleright;

//

// label2

//

This.label2.location = new system.drawing.point (8, 72);

THIS.LABEL2.NAME = "label2";

THIS.LABEL2.SIZE = New System.drawing.size (72, 23);

THIS.Label2.tabindex = 1;

THIS.Label2.text = "Some to:";

THIS.Label2.TextAlign = system.drawing.contentAlignment.middleright;

//

// srcaddress

//

this.srcaddress.location = new system.drawing.point (80, 32);

THIS.SRCADDRESS.NAME = "Srcaddress";

this.srcaddress.size = new system.drawing.size (216, 21);

this.srcaddress.tabindex = 2;

THIS.SRCADDRESS.TEXT = "";

//

// taraddress

//

This.taraddress.location = new system.drawing.point (80, 72);

this.taraddress.name = "traddress";

This.taraddress.size = new system.drawing.size (216, 21);

this.taraddress.tabindex = 3;

THIS.TARADDRESS.TEXT = ""

//

// statusbar

//

THIS.STATUSBAR.LOCATION = New System.drawing.Point (0, 151);

THIS.STATUSBAR.NAME = "statusbar";

THIS.STATUSBAR.SIZE = New System.drawing.size (312, 22);

THIS.STATUSBAR.TABINDEX = 4;

//

// start

//

This.start.Flatstyle = system.windows.Forms.Flatstyle.FLAT;

This.start.location = new system.drawing.point (216, 112);

THIS.Start.name = "start";

This.Start.size = new system.drawing.size (75, 24);

THIS.Start.tabindex = 5;

This.Start.Text = "Start download";

this.start.click = new system.eventhandler (this.start_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

This.ClientSize = New System.drawing.Size (312, 173); this.controls.addrange (new system.windows.forms.control [] {

THIS.START,

this.statusbar,

this.taraddress,

This.SRCADDRESS,

THIS.LABEL2,

THIS.LABEL1});

THIS.MAXIMIZEBOX = FALSE;

THIS.NAME = "Form1";

this.Text = "File Downloader";

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void startdownload ()

{

Start.enabled = false;

String url = srcaddress.text;

INT n = url.lastIndexof ('/');

String Urladdress = url.substring (0, n);

String filename = url.substring (n 1, url.length-n-1);

String dir = traddress.text;

String Path = DIR '//' filename;

Try

{

WebRequest myre = WebRequest.create (Urladdress);

}

Catch (WebException Exp)

{

Messagebox.show (Exp.Message, "Error");

}

Try

{

StatusBar.Text = "Start downloading files ...";

Client.downloadfile (Urladdress, filename);

Stream str = client.openread (Urladdress);

StreamReader Reader = New StreamReader (STR);

BYTE [] mbyte = new byte [100000];

INT AllmyByte = (int) mbyte.length;

INT startmbyte = 0;

StatusBar.Text = "Received data ...";

While (allmybyte> 0)

{

INT m = str.read (mbyte, startmbyte, allmybyte);

IF (m == 0)

Break;

STARTMBYTE = M;

AllmyByte- = m;

}

FILESTREAM FSTR = New FileStream (path, filemode.openorcreate, fileaccess.write);

FSTR.WRITE (Mbyte, 0, Startmbyte);

Str.close ();

FSTR.CLOSE ();

Statusbar.Text = "Download it!";

}

Catch (WebException Exp)

{

Messagebox.show (Exp.Message, "Error");

STATUSBAR.TEXT = "";

}

Start.enabled = true;

}

Private Void Start_Click (Object Sender, System.Eventargs E) {

Thread TH = New Thread (New ThreadStart (StartDownload);

Th.start ();

}

}

}

After the program is complete, the running program is shown below:

(When you start downloading the file)

(The file is downloaded)

four. to sum up:

The above shows how to implement the download of the network file with Visual C # through an example, and we are not difficult to find the Internet communication programming programming with Visual C #. In the above program, we only use some methods of the WebClient class, and the WebClient class is not only providing the method of the network file download, but also provides the method of file upload, interested readers may wish to try - to implement a file Upload. At the same time, this program is just a very simple example. After the program is loaded with a web page, it is just the content of the main page, and the pictures, CSS and other files are not available, so make a better file downloader. Readers need to further improve it.

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

New Post(0)