这个程序实现了不断访问一个网站的功能,这样可以增加网站的访问量,实现了最小化到系统托盘的功能.
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.Text;
using System.Threading;
using System.Diagnostics;
namespace AccessBlog
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.ComponentModel.IContainer components=null;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private ContextMenu menu;
private static int accessAccount;
private System.Windows.Forms.NotifyIcon notifyIcon1;
Thread accessThread;
public Form1()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
InitOther();
}
private void InitOther()
...{
MenuItem[] mnu = new MenuItem[1];
mnu[0] = new MenuItem();
mnu[0].Text = "退出";
mnu[0].Click += new EventHandler(Menu_Click);
menu = new ContextMenu(mnu);
this.notifyIcon1.ContextMenu = menu;
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 23);
this.button1.TabIndex = 0;
this.button1.Text = "访问";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(296, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "http://blog.163.com/hzxmsn/";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 3;
this.label1.Text = "访问地址:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBox2
//
this.textBox2.Enabled = false;
this.textBox2.Location = new System.Drawing.Point(272, 40);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(80, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(208, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 5;
this.label2.Text = "已经访问:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(352, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(24, 23);
this.label3.TabIndex = 6;
this.label3.Text = "次";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 40);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 23);
this.label4.TabIndex = 7;
this.label4.Text = "访问次数:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(64, 40);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(72, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(136, 40);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(24, 23);
this.label5.TabIndex = 9;
this.label5.Text = "次";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "提高网站访问量";
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 77);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "访问网站";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
...{
if(textBox1.Text != "" && textBox3.Text != "")
...{
textBox1.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
accessThread = new Thread(new ThreadStart(AccessBlog));
accessThread.Start();
}
else if(textBox1.Text == "" && textBox3.Text != "")
...{
MessageBox.Show("请输入访问博客地址!");
}
else if(textBox3.Text == "" && textBox1.Text != "")
...{
MessageBox.Show("请输入需要访问次数!");
}
else
...{
MessageBox.Show("请输入访问博客地址和需要访问次数!");
}
}
private void AccessBlog()
...{
string URL = textBox1.Text;
int Count = Convert.ToInt32(textBox3.Text);
try
...{
for(int i=0;i<Count;i++)
...{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.StatusCode == HttpStatusCode.OK)
...{
accessAccount++;
textBox2.Text = accessAccount.ToString();
response.Close();
this.notifyIcon1.Text = "已经访问了"+textBox2.Text+"次";
}
}
}
catch(Exception ex)
...{
throw new Exception(ex.Message);
}
finally
...{
textBox1.Enabled = true;
textBox3.Enabled = true;
button1.Enabled = true;
accessAccount=0;
textBox2.Text = "";
MessageBox.Show("已经访问完:"+Count+"次博客");
this.notifyIcon1.Text = "提高网站访问量";
accessThread.Abort();
}
}
private void Menu_Click(object sender, EventArgs e)
...{
this.notifyIcon1.Visible = false;
this.Close();
}
private void notifyIcon1_Click(object sender, System.EventArgs e)
...{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
private void Form1_SizeChanged(object sender, System.EventArgs e)
...{
if(this.WindowState == FormWindowState.Minimized)
...{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
...{
Process.GetCurrentProcess().Kill();
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace AccessBlog
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.ComponentModel.IContainer components=null;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private ContextMenu menu;
private static int accessAccount;
private System.Windows.Forms.NotifyIcon notifyIcon1;
Thread accessThread;
public Form1()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
InitOther();
}
private void InitOther()
...{
MenuItem[] mnu = new MenuItem[1];
mnu[0] = new MenuItem();
mnu[0].Text = "退出";
mnu[0].Click += new EventHandler(Menu_Click);
menu = new ContextMenu(mnu);
this.notifyIcon1.ContextMenu = menu;
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 23);
this.button1.TabIndex = 0;
this.button1.Text = "访问";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(296, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "http://blog.163.com/hzxmsn/";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 3;
this.label1.Text = "访问地址:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBox2
//
this.textBox2.Enabled = false;
this.textBox2.Location = new System.Drawing.Point(272, 40);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(80, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(208, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 5;
this.label2.Text = "已经访问:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(352, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(24, 23);
this.label3.TabIndex = 6;
this.label3.Text = "次";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 40);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 23);
this.label4.TabIndex = 7;
this.label4.Text = "访问次数:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(64, 40);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(72, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(136, 40);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(24, 23);
this.label5.TabIndex = 9;
this.label5.Text = "次";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "提高网站访问量";
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 77);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "访问网站";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
...{
if(textBox1.Text != "" && textBox3.Text != "")
...{
textBox1.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
accessThread = new Thread(new ThreadStart(AccessBlog));
accessThread.Start();
}
else if(textBox1.Text == "" && textBox3.Text != "")
...{
MessageBox.Show("请输入访问博客地址!");
}
else if(textBox3.Text == "" && textBox1.Text != "")
...{
MessageBox.Show("请输入需要访问次数!");
}
else
...{
MessageBox.Show("请输入访问博客地址和需要访问次数!");
}
}
private void AccessBlog()
...{
string URL = textBox1.Text;
int Count = Convert.ToInt32(textBox3.Text);
try
...{
for(int i=0;i<Count;i++)
...{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.StatusCode == HttpStatusCode.OK)
...{
accessAccount++;
textBox2.Text = accessAccount.ToString();
response.Close();
this.notifyIcon1.Text = "已经访问了"+textBox2.Text+"次";
}
}
}
catch(Exception ex)
...{
throw new Exception(ex.Message);
}
finally
...{
textBox1.Enabled = true;
textBox3.Enabled = true;
button1.Enabled = true;
accessAccount=0;
textBox2.Text = "";
MessageBox.Show("已经访问完:"+Count+"次博客");
this.notifyIcon1.Text = "提高网站访问量";
accessThread.Abort();
}
}
private void Menu_Click(object sender, EventArgs e)
...{
this.notifyIcon1.Visible = false;
this.Close();
}
private void notifyIcon1_Click(object sender, System.EventArgs e)
...{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
private void Form1_SizeChanged(object sender, System.EventArgs e)
...{
if(this.WindowState == FormWindowState.Minimized)
...{
this.Hide();
this.notifyIcon1.Visible = true;
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
...{
Process.GetCurrentProcess().Kill();
}
}
}