📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
namespace WebPageLoad
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private WebClient MyWebClient=new WebClient();
//private System.Windows.Forms.SaveFileDialog SFD=new
/// <summary>
/// 必需的设计器变量。
/// </summary>
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = "URL:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 128);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(40, 24);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(104, 16);
this.radioButton1.TabIndex = 2;
this.radioButton1.Text = "Download";
this.radioButton1.Click += new System.EventHandler(this.RadioBtn_click);
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(40, 48);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(104, 16);
this.radioButton2.TabIndex = 3;
this.radioButton2.Text = "Upload";
this.radioButton2.Click += new System.EventHandler(this.RadioBtn_click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(40, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 24);
this.button2.TabIndex = 5;
this.button2.Text = "Begin";
this.button2.Click += new System.EventHandler(this.BeginBtn_click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(160, 240);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 24);
this.button3.TabIndex = 6;
this.button3.Text = "Exit";
this.button3.Click += new System.EventHandler(this.ExitBtn_click);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioButton2,
this.radioButton1});
this.groupBox1.Location = new System.Drawing.Point(8, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 80);
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Mode";
//
// saveFileDialog1
//
this.saveFileDialog1.FileName = "doc1";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 286);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(304, 16);
this.statusBar1.TabIndex = 8;
this.statusBar1.Text = "Status";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 16);
this.label2.TabIndex = 9;
this.label2.Text = "File Location:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 192);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(192, 21);
this.textBox2.TabIndex = 10;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(216, 192);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 11;
this.button1.Text = "Browse";
this.button1.Click += new System.EventHandler(this.BrowseBtn_click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 302);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox2,
this.label2,
this.statusBar1,
this.groupBox1,
this.button3,
this.button2,
this.textBox1,
this.label1});
this.Name = "Form1";
this.Text = "WebPageLoad";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void RadioBtn_click(object sender, System.EventArgs e)
{
if(radioButton1.Checked==true)
{statusBar1.Text="Ready to download";}
if(radioButton2.Checked==true)
{statusBar1.Text="Ready to upload";}
}
private void BrowseBtn_click(object sender, System.EventArgs e)
{
try
{
if(radioButton1.Checked==false&&radioButton2.Checked==false)
MessageBox.Show("Please choose the mode!");
else
{
if(radioButton1.Checked==true)
{
saveFileDialog1.Filter="Html(*.htm)|*.htm|All Files(*.*)|*.*";
saveFileDialog1.Title="Save as ...";
if(textBox2.Text=="")
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text=saveFileDialog1.FileName;
}
else
{
MessageBox.Show("Please Choose the file!");
}
}
}
if(radioButton2.Checked==true)
{
openFileDialog1.Filter="Html(*.htm)|*.htm|All Files(*.*)|*.*";
openFileDialog1.Title="Open file ...";
if(textBox2.Text=="")
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text=openFileDialog1.FileName;
}
else
{
MessageBox.Show("Please choose the file!");
}
}
}
}
}
catch(Exception ee)
{MessageBox.Show(ee.Message);}
}
private void BeginBtn_click(object sender, System.EventArgs e)
{
string URL=textBox1.Text;
if(radioButton1.Checked==false&&radioButton2.Checked==false)
MessageBox.Show("Please choose the mode!");
else
{
if(radioButton1.Checked==true)
{
try
{
statusBar1.Text="Begin to download";
MyWebClient.DownloadFile(URL,textBox2.Text);
statusBar1.Text="Download Successfully!";
}
catch(WebException ee)
{MessageBox.Show(ee.Message);}
}
if(radioButton2.Checked==true)
{
try
{
statusBar1.Text="Begin to upload";
MyWebClient.UploadFile(URL,textBox2.Text);
statusBar1.Text="Upload Successfully!";
}
catch(WebException ee)
{MessageBox.Show(ee.Message);}
}
}
}
private void ExitBtn_click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -