using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Threading;
namespace GetWebFiles
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tbWebSiteUrl;
private System.Windows.Forms.Button btGo;
private System.Windows.Forms.ListView lvResult;
private System.Windows.Forms.ColumnHeader filename;
private System.Windows.Forms.ColumnHeader result;
private System.Windows.Forms.ColumnHeader errormsg;
private System.Windows.Forms.Button btStop;
private System.Threading.Thread mThread;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btClearAll;
private System.Windows.Forms.Button btClearNotExists;
///
/// 字符数组
///
private string[] strCharsArray = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
///
/// 数字数组
///
private string[] strNumberArray = {"0","1","2","3","4","5","6","7","8","9"};
///
/// 下划线
///
private string[] strUnderLineArray = {"_"};
///
/// 存放最终文件名组成字符的数组
///
private string[] strFinalArray ;
///
/// 探测结果为存在的文件的数量
///
private long nExistsNumber = 0;
///
/// 探测结果为不存在的文件的数量
///
private long nNotExistsNumber = 0;
///
/// 探测中发生错误的文件的数量
///
private long nErrorNumber = 0;
///
/// 总共要探测的文件的数量
///
private long nTotalFileNumber = 0;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox cbFileNameLength;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox tbFileType;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ProgressBar progBar;
private System.Windows.Forms.CheckBox cbOnlySuccess;
private System.Windows.Forms.CheckBox cbNumber;
private System.Windows.Forms.CheckBox cbChar;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.CheckBox cbUnderLine;
private System.Windows.Forms.Button btOutPut;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1 )
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent );
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose bool disposing )
{
if disposing )
{
if components != null)
{
components.Dispose );
}
}
base.Dispose disposing );
}
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main )
{
Application.Run new Form1 ));
}
private void btGo_Click object sender, System.EventArgs e)
{
#region 生成包含文件名字符的数组
string[] strTempArray = new string[37];
int j = 0;
if cbChar.Checked)
{
for int i=0;i {
strTempArray[j] = strCharsArray[i];
j++;
}
}
if cbNumber.Checked)
{
for int i=0;i {
strTempArray[j] = strNumberArray[i];
j++;
}
}
if cbUnderLine.Checked)
{
for int i=0;i {
strTempArray[j] = strUnderLineArray[i];
j++;
}
}
j=0;
for int i=0;i {
if strTempArray[i] != null)
{
j++;
}
}
strFinalArray = new string[j];
string strTemp = "";
for int i=0;i {
strFinalArray[i] = strTempArray[i];
strTemp += strTempArray[i];
}
#endregion
//设置初始值
nExistsNumber = 0;
nNotExistsNumber = 0;
nErrorNumber = 0;
//计算出要探测的文件的总数
nTotalFileNumber = Convert.ToInt64 System.Math.Pow strFinalArray.Length), Convert.ToInt32 cbFileNameLength.SelectedItem.ToString )))));
//设置进度条的初始值
progBar.Step = 1;
progBar.Maximum = int)nTotalFileNumber;
progBar.Value = 0;
//清空结果列表
lvResult.Items.Clear );
////启动线程
mThread = new System.Threading.Thread new System.Threading.ThreadStart GetIt));
mThread.Start );
}
private void btStop_Click object sender, System.EventArgs e)
{
//如果线程还在进行,终止它吧!
if mThread.IsAlive)
{
mThread.Abort );
MessageBox.Show "搜索中止!" + nExistsNumber.ToString ) + "个文件存在," + nNotExistsNumber.ToString ) + "个文件不存在," + nErrorNumber.ToString ) + "个文件探测失败。","搜索完成",MessageBoxButtons.OK,MessageBoxIcon.Information);
nExistsNumber = 0;
nNotExistsNumber = 0;
nErrorNumber = 0;
progBar.Value = 0;
}
}
private void GetIt )
{
int nFileNameLength = Convert.ToInt32 cbFileNameLength.SelectedItem.ToString ));
string strWebSiteUrl = tbWebSiteUrl.Text.Trim );
string strFileType = tbFileType.Text.Trim );
if strWebSiteUrl == "" || strFileType == "" || strWebSiteUrl.Substring 0,7) != "http://")
{//参数不能非法
MessageBox.Show "参数错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
mThread.Abort );
}
else
{
strFileType = "." + strFileType;
//开始探测
int[] nResultArray = new int[nFileNameLength];
string strResult = "";
for int i = 0;i {
nResultArray[i] = 0;
}
for int i = 0;i {
int j = 0;
nResultArray[0] = i;
for j=0;j {
if nResultArray[j] == strFinalArray.Length)
{
if j < nFileNameLength - 1)
{
nResultArray[j+1] += 1;
nResultArray[j] = 0;
}
else
{
if nResultArray[j] == strFinalArray.Length)
{
break;
}
}
}
strResult += strFinalArray[nResultArray[j]];
}
if nResultArray[nFileNameLength - 1] == strFinalArray.Length)
{
break;
}
else
{
//文件名有了,探测吧!
this.DetectIt strWebSiteUrl,strResult,strFileType);
//使进度条向前走1个Step
progBar.PerformStep );
strResult = "";
}
if i == strFinalArray.Length)
{
i = 0;
}
}
// this.DetectIt strWebSiteUrl,strFileType,0,nFileNameLength,strFileNameArray);递归
MessageBox.Show "搜索完成!" + nExistsNumber.ToString ) + "个文件存在," + nNotExistsNumber.ToString ) + "个文件不存在," + nErrorNumber.ToString ) + "个文件探测失败。","搜索完成",MessageBoxButtons.OK,MessageBoxIcon.Information);
progBar.Value = 0;
mThread.Abort );
}
}
private void DetectIt string strPath,string strFileName,string strFileType)
{
/*连接远程站点,探测文件是否存在 */
try
{
WebRequest mRequest = WebRequest.Create strPath + strFileName + strFileType);
WebResponse mResponse = mRequest.GetResponse );
ListViewItem mItem = new ListViewItem strFileName + strFileType);
mItem.SubItems.Add "存在");
lvResult.Items.Add mItem);
nExistsNumber++;
mResponse.Close );
}
catch WebException err)
{
if !cbOnlySuccess.Checked)
{
ListViewItem mItem = new ListViewItem strFileName + strFileType);
mItem.SubItems.Add "不存在");
mItem.SubItems.Add err.Message);
lvResult.Items.Add mItem);
}
nNotExistsNumber++;
}
catch Exception err1)
{
if !cbOnlySuccess.Checked)
{
ListViewItem mItem = new ListViewItem strFileName + strFileType);
mItem.SubItems.Add "探测失败");
mItem.SubItems.Add err1.ToString ));
lvResult.Items.Add mItem);
}
nErrorNumber++;
}
/* end */
}
private void Form1_Closing object sender, System.ComponentModel.CancelEventArgs e)
{
//关闭窗体前先检查线程是否正在运行,如果运行,关闭它
if mThread != null && mThread.IsAlive)
{
mThread.Abort );
}
}
private void btClearAll_Click object sender, System.EventArgs e)
{
lvResult.Items.Clear );
}
///
/// 清除所有结果为不存在记录
///
///
///
private void btClearNotExists_Click object sender, System.EventArgs e)
{
foreach ListViewItem item in lvResult.Items)
{
if item.SubItems[1].Text == "探测失败" || item.SubItems[1].Text == "不存在")
{
item.Remove );
}
}
}
private void Form1_Load object sender, System.EventArgs e)
{
cbFileNameLength.SelectedIndex = 0;
tbWebSiteUrl.Text = "http://localhost/test/";
}
///
/// 导出文件
///
///
///
private void btOutPut_Click object sender, System.EventArgs e)
{
if lvResult.Items.Count == 0)
{
MessageBox.Show "目前没有任何结果!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
if saveFileDialog.ShowDialog ) == DialogResult.OK)
{
//获得文件路径
string strFilePath = saveFileDialog.FileName;
try
{
//写文件
TextWriter tw = File.CreateText strFilePath);
// tw.WriteLine "文件名称\t探测结果\t错误原因\t");
foreach ListViewItem item in lvResult.Items)
{
string strLine = "";
for int i=0;i {
if i == 0)
{
strLine += tbWebSiteUrl.Text + item.SubItems[0].Text + "\t";
}
else
{
strLine += item.SubItems[i].Text + "\t";
}
}
tw.WriteLine strLine);
}
tw.Close );
MessageBox.Show "导出文件成功!","导出成功",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch
{
MessageBox.Show "导出文件过程中发生错误!","异常错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
}
}
|